Search code examples
cocos3d

How to run reversed animation with Cocos3D?


I use below code to run the animation, but how to reversed the animation? (For example, a door model has an open animation, but I want to make it close)

CC3ResourceNode* rezNode = [CC3PODResourceNode nodeFromFile: @"bd1hW1368.POD"];
    [self addChild: rezNode];

    CCActionInterval *stride = [CC3Animate actionWithDuration:10.0];
    [rezNode runAction:[CCRepeatForever actionWithAction:stride]];

[UPDATE]

Related to Bill's answer, I create a continuous door close/open animation as below:

    CC3ResourceNode* rezNode = [CC3PODResourceNode nodeFromFile: @"bd1hW1368.POD"];
    [self addChild: rezNode];

    CC3Animate *stride = [CC3Animate actionWithDuration:10.0];
    CC3Animate *reversedStride = [CC3Animate actionWithDuration:10.0];
    reversedStride.isReversed = YES;

    CCRepeatForever *repeat = [CCRepeatForever actionWithAction:[CCSequence actionWithArray:@[stride, reversedStride]]];
    [rezNode runAction:repeat];

Solution

  • Like most CCActionInterval subclasses, CC3Animate supports the reverse method, which returns a new CC3Animate instance configured to run backwards.

    You can also reuse the same CC3Animate instance and set the isReversed property to YES, but creating a separate instance using the reverse method will allow you to more easily do things like sequence a door open action followed by a door close action.