[Cocos2d-x相关教程来源于红孩儿的游戏编程之路CSDN博客地址:http://blog.csdn.net/honghaier]
红孩儿Cocos2d-X学习园地QQ2群:44208467加群写:Cocos2d-x
红孩儿Cocos2d-X学习园地QQ群:249941957 [暂满]加群写:Cocos2d-x
本章为我的Cocos2d-x教程一书初稿。望各位看官多提建议!
另请转载者注明转载地址及作者。
Cocos2d-x2.0 之 ClickAndMoveTest “谈不上深入”的分析.
另:本章所用Cocos2d-x版本为:
cocos2d-2.0-x-2.0.2@ Aug 30 2012
http://cn.cocos2d-x.org/download
大家好,经过前一阶段复杂的,长篇大论的动画部分的讲解,我们对于Cocos2d-x的基本结构有了一个大概的认识,也更加增强了我们继续将Cocos2d-x的示例学习完的信心,我们今天轻松一下,来学习一下ClickAndMoveTest这个例子。因为这个示例代码较短,谈不上深入,所以之后会再讲解一篇RotateWorldTest,以满足大家的胃口。
打开工程中TestCpp下的ClickAndMoveTest目录,可以看到两个文件:ClickAndMove.h和cpp。
打开ClickAndMove.h:
与之前一样,建立了一个场景,并在场景中创建所需要表现的CCLayer。
- class ClickAndMoveTestScene : public TestScene
- {
- public:
- virtual void runThisTest();
- };
-
- class MainLayer : public CCLayer
- {
- public:
- MainLayer();
- virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
- };
可以看到,这里面最关键的是在MainLayer中重载了基类的相应接口函数:
virtualvoid ccTouchesEnded(CCSet*pTouches, CCEvent *pEvent);
CCLayer层本身由CCTouchDelegate派生,CCTouchDelegate是触点消息响应接口类,它指定了CCLayer在响应触点事件时所触发的函数。
- class CC_DLL CCTouchDelegate
- {
- public:
-
- CCTouchDelegate() {}
-
- virtual ~CCTouchDelegate()
- {
- }
-
-
- virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false;};
-
- virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
-
- virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
-
- virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
-
-
- virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
- virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
- virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
- virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
-
- };
看一下这些接口,如果我们需要增加相应的触屏事件处理,只需要在CCLayer的派生类中重载这些接口函数就OK了。
而MainLayer重载的virtualvoid ccTouchesEnded(CCSet *pTouches, CCEvent*pEvent);
即为多点触屏事件中的停止移动时触发的回调函数。
我们继续分析CPP文件:
-
- enum
- {
- kTagSprite = 1,
- };
-
- void ClickAndMoveTestScene::runThisTest()
- {
-
- CCLayer* pLayer = new MainLayer();
- pLayer->autorelease();
-
- addChild(pLayer);
- CCDirector::sharedDirector()->replaceScene(this);
- }
-
- MainLayer::MainLayer()
- {
-
- setTouchEnabled(true);
-
- CCSprite* sprite = CCSprite::create(s_pPathGrossini);
-
- CCLayer* layer = CCLayerColor::create(ccc4(255,255,0,255));
-
- addChild(layer, -1);
-
- addChild(sprite, 0, kTagSprite);
-
- sprite->setPosition( CCPointMake(20,150) );
-
- sprite->runAction( CCJumpTo::create(4, CCPointMake(300,48), 100, 4) );
-
- layer->runAction( CCRepeatForever::create(
- (CCActionInterval*)( CCSequence::create(
- CCFadeIn::create(1),
- CCFadeOut::create(1),
- NULL) )
- ) );
- }
-
- void MainLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
- {
-
- CCSetIterator it = pTouches->begin();
- CCTouch* touch = (CCTouch*)(*it);
-
-
- CCPoint location = touch->getLocation();
-
- CCNode* s = getChildByTag(kTagSprite);
-
- s->stopAllActions();
-
- s->runAction( CCMoveTo::create(1, CCPointMake(location.x, location.y) ) );
-
- float o = location.x - s->getPosition().x;
- float a = location.y - s->getPosition().y;
-
- float at = (float) CC_RADIANS_TO_DEGREES( atanf( o/a) );
-
- if( a < 0 )
- {
- if( o < 0 )
- at = 180 + fabs(at);
- else
- at = 180 - fabs(at);
- }
-
- s->runAction( CCRotateTo::create(1, at) );
- }
这个示例比较简单,大家也可以参考《Cocos2d-x 2.0 百例精讲:如何让一个精灵跟随触点移动》一文进行学习。