cocos2dx为Sprite添加触摸事件监听器

1.首先头文件定义事件处理的函数原型

private:
bool onTouchBegan(Touch* tTouch,Event* eEvent);//手指按下事件
void onTouchMoved(Touch* tTouch,Event* eEvent);//手指移动事件
void onTouchEnded(Touch* tTouch,Event* eEvent);//手指离开事件

2.实现原型

bool ShopItem::onTouchBegan(Touch* tTouch,Event* eEvent){
 if (sprite->getBoundingBox().containsPoint(tTouch->getLocation())){//判断触摸点是否在目标的范围内     /**这里为事件内容**/
    return true;
 }else
    return false;
 }
} void ShopItem::onTouchMoved(Touch* tTouch,Event* eEvent){
/**这里为事件内容**/
} void ShopItem::onTouchEnded(Touch* tTouch,Event* eEvent){
/**这里为事件内容**/
}

3.绑定事件

auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(ShopItem::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(ShopItem::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(ShopItem::onTouchEnded, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, sprite);

  

上一篇:Android线程之异步消息处理机制(二)——Message、Handler、MessageQueue和Looper


下一篇:mybatis的collection查询问题以及使用原生解决方案的结果