首页 > 代码库 > cocos2d-x学习记录2——CCAction动作

cocos2d-x学习记录2——CCAction动作

CCAction能够使CCNode运动起来,能够呈现出多种多样的动作。这些动作能够改变其运动方向、形状、大小、旋转等。

同时,还可利用CCCallFunc、CCCallFuncN、CCCallFuncO、CCCallFuncND等函数实现回调。

MyScene.h代码

 1 #ifndef MyScene_H_H 2 #define MyScene_H_H 3  4 #include "cocos2d.h" 5 using namespace cocos2d; 6  7 class MyScene : public CCLayer 8 { 9 public:10     static CCScene* createScene();11     virtual bool init();12     CREATE_FUNC( MyScene );13 14     void callback1();15     void callback2(CCNode *node);16     void callback3(CCObject *obj);17     void callback4(CCNode *node, void *data);18 19 20 private:21 };22 23 #endif

MyScene.cpp代码

 1 #include "MyScene.h" 2  3 CCScene* MyScene::createScene() 4 { 5     CCScene *scene = CCScene::create(); 6     MyScene *layer = MyScene::create(); 7     scene->addChild(layer); 8     return scene; 9 };10 11 12 bool MyScene::init()13 {14     if( !CCLayer::init() ){15         return false;16     }17 18     CCSize size = CCDirector::sharedDirector()->getWinSize();19     CCSprite *sprite = CCSprite::create("pal4.png");20     sprite->setAnchorPoint( ccp(0.5, 0.5) );21     //sprite->setPosition( ccp(size.width/2, size.height/2) );22     sprite->setPosition( ccp(0, 0) );23     sprite->setScale(0.5f);24     sprite->setTag(2345);25     addChild(sprite);26 27     CCAction *action1 = CCMoveTo::create(1, ccp(size.width-sprite->getContentSize().width/2, size.height-sprite->getContentSize().height/2));28     CCAction *action2 = CCRotateBy::create(2, 360);29     CCAction *action3 = CCBlink::create(2, 5);30     CCAction *action4 = CCCallFunc::create( this, callfunc_selector(MyScene::callback1) );31     CCAction *action5 = CCCallFuncN::create( this, callfuncN_selector(MyScene::callback2) );32     CCAction *action6 = CCCallFuncO::create( this, callfuncO_selector(MyScene::callback3), sprite );33     CCAction *action7 = CCCallFuncND::create( this, callfuncND_selector(MyScene::callback4), "funcND" );34 35     CCArray *array = CCArray::createWithCapacity(8);36     array->addObject(action1);37     array->addObject(action2);38     array->addObject(action3);39     array->addObject(action4);40     array->addObject(action5);41     array->addObject(action6);42     array->addObject(action7);43     //array->addObject(NULL);44     //array->retain();45     //CCAction *totoalAction = CCSequence::create( action1, action2, action3, action4, action5, action6, action7, NULL );46     CCAction *totalAction = CCSequence::create(array);47     sprite->runAction(totalAction);48 49     return true;50 }51 52 void MyScene::callback1()53 {54     CCLog( "callback1" );55 }56 57 void MyScene::callback2(CCNode *node)58 {59     CCLog( "%d", node->getTag() );60 }61 62 void MyScene::callback3(CCObject *obj)63 {64     CCNode *node = dynamic_cast<CCNode*>(obj);65     if( node != NULL ){66         CCLog( "%d", node->getTag() );67     }68 }69 70 void MyScene::callback4(CCNode *node, void *data)71 {72     CCLog( "%d, %s", node->getTag(), data );73     CCMessageBox( "Over!", "Info" );74 }

callback1到callback4分别对应4种回调形式,只是各自带的参数不同而已。

运行结果:

  

cocos2d-x学习记录2——CCAction动作