Cocos2d-x 动作

1. CCToggleVisibility 可见切换

如果当前CCNode对象为可见,那么执行此动作之后就会进入不可见的状态。原理是通过修改CCNode对象中的Visiable属性。

sprite->runAction(CCToggleVisibility::create());

void ActionReverseSequence2::onEnter()
{
    ActionsDemo::onEnter();

    alignSpritesLeft(2);


    // Test:
    //   Sequence should work both with IntervalAction and InstantActions
    CCActionInterval*  move1 = CCMoveBy::create(1, ccp(250,0));
    CCActionInterval*  move2 = CCMoveBy::create(1, ccp(0,50));
    CCToggleVisibility*  tog1 = new CCToggleVisibility();
    CCToggleVisibility*  tog2 = new CCToggleVisibility();
    tog1->autorelease();
    tog2->autorelease();
    CCFiniteTimeAction*  seq = CCSequence::create( move1, tog1, move2, tog2, move1->reverse(), NULL);
    CCActionInterval*  action = CCRepeat::create(CCSequence::create( seq, seq->reverse(), NULL), 3);



    // Test:
    //   Also test that the reverse of Hide is Show, and vice-versa
    m_kathia->runAction(action);

    CCActionInterval*  move_tamara = CCMoveBy::create(1, ccp(100,0));
    CCActionInterval*  move_tamara2 = CCMoveBy::create(1, ccp(50,0));
    CCActionInstant*  hide = CCHide::create();
    CCFiniteTimeAction*  seq_tamara = CCSequence::create( move_tamara, hide, move_tamara2, NULL);
    CCFiniteTimeAction*  seq_back = seq_tamara->reverse();
    m_tamara->runAction( CCSequence::create( seq_tamara, seq_back, NULL));
}

 

 
2.CCTargetedAction 改变动作执行对象
通常默认的动作执行对象是调用runAction的对象,而CCTargetedAction可以改变动作执行对象
void ActionTargeted::onEnter()
{
    ActionsDemo::onEnter();
    centerSprites(2);


    CCJumpBy* jump1 = CCJumpBy::create(2,CCPointZero,100,3);
    CCJumpBy* jump2 = (CCJumpBy*)jump1->copy()->autorelease();
    CCRotateBy* rot1 =  CCRotateBy::create(1, 360);
    CCRotateBy* rot2 = (CCRotateBy*)rot1->copy()->autorelease();

    CCTargetedAction *t1 = CCTargetedAction::create(m_kathia, jump2);
    CCTargetedAction *t2 = CCTargetedAction::create(m_kathia, rot2);


    CCSequence* seq = (CCSequence*)CCSequence::create(jump1, t1, rot1, t2, NULL);
    CCRepeatForever *always = CCRepeatForever::create(seq);

    m_tamara->runAction(always);
}

 

 
3.CCFollow 设置一个节点跟随另一个节点
void ActionFollow::onEnter()
{
    ActionsDemo::onEnter();

    centerSprites(1);

    CCSize s = CCDirector::sharedDirector()->getWinSize();

    m_grossini->setPosition(ccp(-200, s.height / 2));
    CCActionInterval* move      = CCMoveBy::create(2, ccp(s.width * 3, 0));
    CCActionInterval* move_back = move->reverse();
    CCSequence* seq       = CCSequence::create(move, move_back, NULL);
    CCAction* rep               = CCRepeatForever::create(seq);

    m_grossini->runAction(rep);

    this->runAction(CCFollow::create(m_grossini, CCRectMake(0, 0, s.width * 2 - 100, s.height)));
}

 

 4.暂停所有动作

 CCSet * m_pPausedTargets = director->getActionManager()->pauseAllRunningActions();

回复动作

 director->getActionManager()->resumeTargets(m_pPausedTargets);

 

 

你可能感兴趣的:(Cocos2d-x 动作)