cocos2d 版本2.2.1
参考资料:
http://blog.csdn.net/xiangzilv1987/article/details/7783845
http://blog.csdn.net/onerain88/article/details/7550009
http://blog.sina.com.cn/s/blog_60906b730100w30v.html
http://blog.sina.com.cn/s/blog_73df887801019fqd.html
添加简单的layer层的触摸事件。。单点触摸
在layer的初始化的init()函数里面添加
//打开触摸开关 setTouchEnabled(true); //2.2.1版本有修改http://www.cnblogs.com/TopWin/archive/2012/09/12/2682042.html CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
然后在layer的。h文件里面添加
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
如果不想输入cocos2d::CCTouch这样的形式,就必须在 include之后添加一句 USING_NS_CC;来使用cocos2d命名空间
然后在layer的cpp文件里面重写这四个函数
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { bool bRet = true; CCLog("HelloWorld Touch Began"); return bRet; } void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLog("HelloWorld Touch Moved"); } void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLog("HelloWorld Touch Ended"); } void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLog("HelloWorld Touch Cancelled"); }