cocos2d-x的触碰事件

头文件:

//在头文件引入,不要在CPP引入,不然会编译不通过
using namespace cocos2d;
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);


	virtual void onEnter();
	virtual void onExit();

CPP文件

//按下
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	CCLog("ccTouchBegan");
	return true;
}
//移动
void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
	CCLog("ccTouchMoved");
}
//触碰停止
void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
	
	//获取坐标
	//pTouch->getLocation();
	CCLog("ccTouchEnded");
}

//建议将触碰委托写在onEnter()和onExit()函数,避免因为没有及时移除委托而报错
void HelloWorld::onEnter()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
	CCLayer::onEnter();
}

void HelloWorld::onExit()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}

 再次说明:建议将触碰委托写在onEnter()和onExit()函数,避免因为没有及时移除委托而报错

相关推荐