CCSequence not working when defined separately

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 10:44:04

问题


In cocos2d-x, I have encountered the following problem when trying to define a CCSequence separately, i.e. not within runAction.

This works:

sprWheel1->runAction(   CCSequence::actions(
    CCDelayTime::actionWithDuration( fDelayTime ),
    CCEaseExponentialOut::actionWithAction( 
        CCRotateBy::actionWithDuration( fMoveTime, fAngle ) ),
    NULL
) );
sprWheel2->runAction(   CCSequence::actions(
    CCDelayTime::actionWithDuration( fDelayTime ),
    CCEaseExponentialOut::actionWithAction( 
        CCRotateBy::actionWithDuration( fMoveTime, fAngle ) ),
    NULL
) );

This does not work:

CCFiniteTimeAction* actRotate = CCSequence::actions(
    CCDelayTime::actionWithDuration( fDelayTime ),
    CCEaseExponentialOut::actionWithAction( 
        CCRotateBy::actionWithDuration( fMoveTime, fAngle ) ),
    NULL
);

sprWheel1->runAction( actRotate );
sprWheel2->runAction( actRotate );

It does not cause a compiler error, or crash, or anything, it just doesn't rotate the sprite.

How can I fix this? (I am using this action multiple times, so it would really help to keep my code cleaner if I could define it only once)


回答1:


A single instance of CCAction should not be used concurrently on multiple objects. The objects contained within the sequence maintain state on the current execution of the action, thus concurrent use on multiple objects will cause mayhem (cocos is possibly 'guarding' silently against that by stoping all actions, not certain). Best to have separate sequences for each sprite you want to animate. If you are concerned about code readability, just create a method in that class that will always return a fresh instance of the sequence.



来源:https://stackoverflow.com/questions/12872958/ccsequence-not-working-when-defined-separately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!