Search code examples
cocos2d-xccspriteccrotateby

cocos2d-x: Rotating CCSprite while moving changes it's final position


I'm trying to make an animation for my game, of an ant leaving it's nest. It should descend the nest going right and down, facing down and then look straight again.

I created two sequences of actions, one for moving and the other for rotating and put them together in a CCSpawn. If I execute only the moving action it works fine, but when the actions are executed together, at the end the sprite moves back to it's original position.

I have no idea of what's happening. Can you guys help?

CCLayer * gameLayer = CCLayer::create();

Ant* ant = Ant::create(); // Ant is a CCSprite

CCPoint nestp = ccp(45, 172);

ant->setPosition(nestp);

gameLayer->addChild(ant);

addChild(gameLayer);

    // ant walking animation
CCAnimate * antWalk = CCAnimate::create(_antWalk);
ant->runAction(CCRepeatForever::create(antWalk));   

CCPoint p1 = ccp(55, 165), p2 = ccp(75,160), p3 = ccp(90,110), p4 = ccp(105, 50);   

CCSequence *moveOut = (CCSequence *)CCSequence::create(CCMoveTo::create(0.3, p1), CCMoveTo::create(0.3, p2), CCMoveTo::create(0.7, p3), CCMoveTo::create(0.7, p4), NULL);

CCSequence *rotateOut = (CCSequence *) CCSequence::create(CCRotateTo::create(0.5, 50), CCDelayTime::create(1), CCRotateTo::create(0.5, 0));

CCSpawn *leaveNest = (CCSpawn *)CCSpawn::create(moveOut, rotateOut, NULL);

ant->runAction(leaveNest);

Solution

  • When reviewing my own question I realized that I had forgotten to add NULL to the end of the rotating sequence. It should be like this:

    CCSequence *rotateOut = (CCSequence *) CCSequence::create(CCRotateTo::create(0.5, 50), CCDelayTime::create(1), CCRotateTo::create(0.5, 0), NULL);
    

    I wouldn't expect this behaviour, as there were no errors, that's why I took so long to find out but now it works fine! I hope it'll help someone running in the same issue.