How do I cancel my move action if another starts?

For now I just have a sprite moving around the screen to the position I touch. It works fine however if make the sprite move a long distance, and while it’s moving that long distance I click to interrupt it’s action and make it move to a new position, it will move to the new position then once done it will pop back to the previous action of the long move. How can I make any previous move commands cancel out if a new one is issued?

This is my current move function:

pSprite->runAction( CCSequence::actions( CCMoveTo::actionWithDuration( moveDuration, position ), CCCallFuncN::actionWithTarget(mainFrame, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) );

And this is what I tried to change it to. I thought adding a tag number to the action, then stopping that tag would work, but it didn’t fix my issue.

pSprite->stopActionByTag(99);
CCMoveTo *moveAction = CCMoveTo::actionWithDuration( moveDuration, position );
moveAction->setTag(99);
pSprite->runAction( CCSequence::actions( moveAction, CCCallFuncN::actionWithTarget(mainFrame, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) );

I just tried pSprite->stopAllActions() before issuing the new move action, however I really don’t want to use this method because in the future I will have this sprite rotating and doing other things and don’t want to stop those actions, only the move action. Thanks for the help guys.

You don’t set the tag of CCSequence.

How do I do it then? Tried searching within the sample projects and can’t answer my own question. Can you please give me an example with my code above? Thank you.

CCNode::stopAllActions()

1 Like

I want to stop only the move action, not the other actions that are going, such as rotation. How can I only stop one of the actions, not all of them?

1 Like

stopActionByTag is the correct way. Does it work for you?

The code I have above, where I set the tag and stop by tag does not work. The move action continues, it does not stop. Only thing that stops it is stopAllActions() but like I said, I don’t want to stop all actions, only the move.

1 Like

Oh, I misunderstood your meaning.
You can not stop a action which is a member of a sequence.

What if I want to stop the entire sequence itself?

@
CCSequence *pSequence = CCSequence::actions( CCMoveTo::actionWithDuration( moveDuration, position ), CCCallFuncN::actionWithTarget(mainFrame, callfuncN_selector(HelloWorld::spriteMoveFinished));

pSprite->runAction( pSequence, NULL) );@

Can I do this later?

pSprite->stopAction( pSequence );

The moveAction is not the direct child of pSprite. You can use stopActionByTag to stop the whole CCSequence, which contains the moveAction.

Amazed that 7 years later, cocos2dx still cannot stop the actions of a running sequence by the sequence tag, nor the actions members of the sequence with them action tags… :frowning: