简单说明cocos2d的触摸事件中如何确定选中对象
2014-03-26 14:44
525 查看
bool Paddle::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
if (m_state != kPaddleStateUngrabbed) return false;
if ( !containsTouchLocation(touch) ) return false;
m_state = kPaddleStateGrabbed;
return true;
}
bool Paddle::containsTouchLocation(CCTouch* touch)
{
return rect().containsPoint(convertTouchToNodeSpaceAR(touch));
}
CCRect Paddle::rect()
{
CCSize s = getTexture()->getContentSize();
return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
以上代码是cocos2d testCpp项目中的代码 , 简单的说明自己对他的理解 大牛请略过
我做了测试当我点击一个对象的时候 cocos2d底层会把所有的在eventDispather注册的监听事件一个一个往上抛,(本人之前是学as的 所有会用as语言来做对比) 他不象as给确定对象发送回调函数
touch中有一个属性 点 他是根据你点击对象的锚点进行计算的 所以我们只要判断这个点是否在这个范围之内 就可以确定这个点击事件是否发上在点击的对象上
(里面很多细节 太懒 不想说)
以下是我自己做的一个实例
CCRect CardsComponent::rect()
{
CCSize s = m_cards->getTexture()->getContentSize();
return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
bool CardsComponent::containsTouchLocation(CCTouch* touch)
{
return rect().containsPoint(convertTouchToNodeSpaceAR(touch));
}
//触控点击
bool CardsComponent::onTouchBegan(Touch* touch, Event* event)
{
if (m_fightStat == Define::CARD_WAIT) //这张牌是否可以用
{
return false;
}
if (!containsTouchLocation(touch)) return false;
CCLog("mouse Down");
m_choiceStat = Define::CARD_SELECT;
return true;
}
void CardsComponent::onTouchEnded(Touch* touch, Event* event)
{
CCLog("mouse Up");
if (m_choiceStat == Define::CARD_SELECT)
{
//Point touchPoint = touch->getLocation();
m_choiceStat = Define::CARD_NOSELECT;
//判断条件 是否放置在战斗场景上
CCNotificationCenter::sharedNotificationCenter()->postNotification(NotificationName::CARD_CHOICE_FIGHT, this);
setPosition(ccp(m_startPoint.x, m_startPoint.y));
}
}
void CardsComponent::onTouchMoved(Touch* touch, Event* event)
{
Point touchPoint = touch->getLocation();
if (m_choiceStat == Define::CARD_SELECT)
{
Point point = ccp(touchPoint.x, touchPoint.y);
setPosition(point);
//takeInGuildLocal(touchPoint.y);
CCNotificationCenter::sharedNotificationCenter()->postNotification(NotificationName::CARD_LOCAL_NOW, touch);
}
}
{
if (m_state != kPaddleStateUngrabbed) return false;
if ( !containsTouchLocation(touch) ) return false;
m_state = kPaddleStateGrabbed;
return true;
}
bool Paddle::containsTouchLocation(CCTouch* touch)
{
return rect().containsPoint(convertTouchToNodeSpaceAR(touch));
}
CCRect Paddle::rect()
{
CCSize s = getTexture()->getContentSize();
return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
以上代码是cocos2d testCpp项目中的代码 , 简单的说明自己对他的理解 大牛请略过
我做了测试当我点击一个对象的时候 cocos2d底层会把所有的在eventDispather注册的监听事件一个一个往上抛,(本人之前是学as的 所有会用as语言来做对比) 他不象as给确定对象发送回调函数
touch中有一个属性 点 他是根据你点击对象的锚点进行计算的 所以我们只要判断这个点是否在这个范围之内 就可以确定这个点击事件是否发上在点击的对象上
(里面很多细节 太懒 不想说)
以下是我自己做的一个实例
CCRect CardsComponent::rect()
{
CCSize s = m_cards->getTexture()->getContentSize();
return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
bool CardsComponent::containsTouchLocation(CCTouch* touch)
{
return rect().containsPoint(convertTouchToNodeSpaceAR(touch));
}
//触控点击
bool CardsComponent::onTouchBegan(Touch* touch, Event* event)
{
if (m_fightStat == Define::CARD_WAIT) //这张牌是否可以用
{
return false;
}
if (!containsTouchLocation(touch)) return false;
CCLog("mouse Down");
m_choiceStat = Define::CARD_SELECT;
return true;
}
void CardsComponent::onTouchEnded(Touch* touch, Event* event)
{
CCLog("mouse Up");
if (m_choiceStat == Define::CARD_SELECT)
{
//Point touchPoint = touch->getLocation();
m_choiceStat = Define::CARD_NOSELECT;
//判断条件 是否放置在战斗场景上
CCNotificationCenter::sharedNotificationCenter()->postNotification(NotificationName::CARD_CHOICE_FIGHT, this);
setPosition(ccp(m_startPoint.x, m_startPoint.y));
}
}
void CardsComponent::onTouchMoved(Touch* touch, Event* event)
{
Point touchPoint = touch->getLocation();
if (m_choiceStat == Define::CARD_SELECT)
{
Point point = ccp(touchPoint.x, touchPoint.y);
setPosition(point);
//takeInGuildLocal(touchPoint.y);
CCNotificationCenter::sharedNotificationCenter()->postNotification(NotificationName::CARD_LOCAL_NOW, touch);
}
}
相关文章推荐
- Cocos2d-js 触摸事件的简单封装
- 已知一指针p,你可以确定该指针是否指向一个有效的对象吗?如果可以,如何确定?如果不可以,请说明原因。
- cocos2d-x 一道简单面试题,触摸事件的重新分发
- cocos2d-x 一道简单面试题,触摸事件的重新分发
- [cocos2d-x 3.6]如何实现“侦听触摸事件和侦听按键事件”,触摸回调函数分别在什么情况下执行
- 一个简单实例理解cocos2d-x触摸事件中的坐标系
- 下面的示例演示如何使用传递到事件处理方法的 GridViewCommandEventArgs 对象确定引发事件的按钮的命令名。
- 《初学cocos2d-x》- 简单的触摸事件实现(5)
- 已知一指针p,你可以确定该指针是否指向一个有效的对象吗?如果可以,如何确定?如果不可以,请说明原因。
- cocos2d-x v3.x Lua 中 [cc.Layer] 如何不让触摸事件向下转递
- cocos2d-x 一道简单面试题,触摸事件的重新分发
- 如何使客户更简单透明地使用单个对象和组合对象——组合模式
- 深入理解JavaScript的闭包特性 如何给循环中的对象添加事件
- Cocos2d-x 3.0 新特性体验-触摸事件处理机制
- 转:如何在运行时确定对象类型(RTTI)
- 如何在运行时确定对象类型(RTTI)
- cocos2d-lua里 如何绑定按钮button的点击事件click
- Flash/Flex学习笔记(35):如何正确监听Stage对象的事件
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- 如何设定按钮的客户端的确定或取消事件