// // HelloWorldScene.h // #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" #include "TestLayer.h" USING_NS_CC; USING_NS_CC_EXT; class HelloWorld : public cocos2d::cclayer { public: virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(HelloWorld); // cclayer已经实现CCTouchDelegate协议,所以cclayer的子类不用再次调用此接口 // 重写“单点”触摸回调函数 virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent); virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent); virtual void ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent); // 重写生命周期函数 virtual void onEnter(); virtual void onExit(); }; #endif
// // HelloWorldScene.cpp // #include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene() { CCScene *scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !cclayer::init() ) { return false; } CCSize winSize = CCDirector::sharedDirector()->getWinSize(); return true; } void HelloWorld::onEnter() { cclayer::onEnter(); CCDirector::sharedDirector()->getTouchdispatcher()->addTargetedDelegate(this,false); // 增加“单点”触摸代理 /* 使用导演获得触摸事件调度者,给它添加一个代理,代理为this */ } void HelloWorld::onExit() { cclayer::onExit(); CCDirector::sharedDirector()->getTouchdispatcher()->removeDelegate(this); /* 使用导演获得触摸事件调度者,把当前的代理this删除掉 */ } bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) { cclOG("HelloWorld::ccTouchBegan"); return true; } void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) { cclOG("HelloWorld::ccTouchMoved"); } void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) { cclOG("HelloWorld::ccTouchEnded"); } void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) { cclOG("HelloWorld::ccTouchCancelled"); }