Cocos2d-x 学习笔记1 - ActionManager

// Example 1
// Move from center of the screen by [150, 0], and then scale by 2 when moving is done
// The difference between MoveTo & MoveBy is the absolute position or delta postion
// The rest of the code is pretty much self-explained.
void
LogicTest::onEnter() { ActionManagerTest::onEnter(); auto grossini = Sprite::create(s_pathGrossini); addChild(grossini, 0, 2); grossini->setPosition(VisibleRect::center()); grossini->runAction( Sequence::create( MoveBy::create(1, Vec2(150,0)), CallFuncN::create(CC_CALLBACK_1(LogicTest::bugMe,this)), nullptr) //1 is the duration, the 2nd arg is the delta postion ); } void LogicTest::bugMe(Node* node) { node->stopAllActions(); node->runAction(ScaleTo::create(2, 2));//The first 2 is the duration ,the second is the scale factor }

 

//Example2:
//Wait 5 seconds then do the action(move in this case)
void
PauseTest::onEnter() { ActionManagerTest::onEnter(); auto l = Label::createWithTTF("After 5 seconds grossini should move", "fonts/Thonburi.ttf", 16.0f); addChild(l); l->setPosition(VisibleRect::center().x, VisibleRect::top().y-75); // // Also, this test MUST be done, after [super onEnter] // auto grossini = Sprite::create(s_pathGrossini); addChild(grossini, 0, kTagGrossini);//kTagGrossini is a enum, which equals to 0 grossini->setPosition(VisibleRect::center() ); auto action = MoveBy::create(1, Vec2(150,0)); auto director = Director::getInstance(); director->getActionManager()->addAction(action, grossini, true);
   schedule( schedule_selector(PauseTest::unpause),
3); //3 is the num of times should be called within one second } void PauseTest::unpause(float dt) { unschedule( schedule_selector(PauseTest::unpause) );//Unschedule the custom selector auto node = getChildByTag( kTagGrossini );//fetch by tag auto director = Director::getInstance(); director->getActionManager()->resumeTarget(node); }

 

//Example 3 // Multiple actions & repeatforever & scheduleonce

void StopAllActionsTest::onEnter()

{

    ActionManagerTest::onEnter();

    

    auto l = Label::createWithTTF("Should stop scale & move after 4 seconds but keep rotate", "fonts/Thonburi.ttf", 16.0f);

    addChild(l);

    l->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - 75) );

    

    //Action1(Sequence)

    auto pMove1 = MoveBy::create(2, Vec2(200, 0));

    auto pMove2 = MoveBy::create(2, Vec2(-200, 0));

    auto pSequenceMove = Sequence::createWithTwoActions(pMove1, pMove2);

    auto pRepeatMove = RepeatForever::create(pSequenceMove);

    pRepeatMove->setTag(kTagSequence);//Each action is assigned with a unique label //Action2(Sequence)

    auto pScale1 = ScaleBy::create(2, 1.5);

    auto pScale2 = ScaleBy::create(2, 1.0/1.5);

    auto pSequenceScale = Sequence::createWithTwoActions(pScale1, pScale2);

    auto pRepeatScale = RepeatForever::create(pSequenceScale);

    pRepeatScale->setTag(kTagSequence);

    

    //Action 3

    auto pRotate = RotateBy::create(2, 360);

    auto pRepeatRotate = RepeatForever::create(pRotate);//will repeat forever unless stopaction is called



    auto pChild = Sprite::create(s_pathGrossini);

    pChild->setPosition( VisibleRect::center() );

    addChild(pChild, 1, kTagGrossini);



    //Run all three actions

    pChild->runAction(pRepeatMove);

    pChild->runAction(pRepeatScale);

    pChild->runAction(pRepeatRotate);

    //4 is the delay time, which means the selector will be called 4 seconds after the animation //this applies in such cases when the action is set as RepeatForever

    this->scheduleOnce((SEL_SCHEDULE)&StopAllActionsTest::stopAction, 4);

}

 

//Example4 //Pause and resume

void ResumeTest::onEnter()

{

    ActionManagerTest::onEnter();



    auto l = Label::createWithTTF("Grossini only rotate/scale in 3 seconds", "fonts/Thonburi.ttf", 16.0f);

    addChild(l);

    l->setPosition(VisibleRect::center().x, VisibleRect::top().y - 75);



    auto pGrossini = Sprite::create(s_pathGrossini);

    addChild(pGrossini, 0, kTagGrossini);

    pGrossini->setPosition(VisibleRect::center());



    pGrossini->runAction(ScaleBy::create(2, 2));



    auto director = Director::getInstance();

    director->getActionManager()->pauseTarget(pGrossini);//the SPRITE(not action) is paused 

    pGrossini->runAction(RotateBy::create(2, 360));



    this->schedule(schedule_selector(ResumeTest::resumeGrossini), 3.0f);// resumeGrossini will be called 3 seconds later

}



void ResumeTest::resumeGrossini(float time)

{

    this->unschedule(schedule_selector(ResumeTest::resumeGrossini));



    auto pGrossini = getChildByTag(kTagGrossini);

    auto director = Director::getInstance();

   director->getActionManager()->resumeTarget(pGrossini);//resume SPRITE

}

 

你可能感兴趣的:(cocos2d-x)