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);

  

posted on 2015-03-02 17:39  益达的博客  阅读(1178)  评论(0编辑  收藏  举报

导航