How to remove node after running action[s]?

Forums Programming cocos2d support (graphics engine) How to remove node after running action[s]?

This topic contains 6 replies, has 4 voices, and was last updated by  neophit 3 years, 3 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
Author Posts
Author Posts
January 14, 2011 at 2:45 am #227555

binarymelon
Participant
@binarymelon

How can I run two parallel actions on a node and remove the node from it’s parent once both actions have completed?

January 14, 2011 at 3:02 am #310412

Omegas7
Participant
@omegas7

Your problem is finding out when both actions are completed?

Well, in both actions, you might make them CCSequences, where the last action will trigger a function that will, dunno, increase the Tag value of the sprite.

So in that same function you can check if the sprite’s tag value is 2 or more. If it is, remove it from the scene since you already know it executed the two actions.

Normally using finding children through Tag values can be a bit slow, but in this case it won’t matter since you’re not using the Tag to find children, just using it as a practical variable XD (silly)

January 14, 2011 at 3:07 am #310413

neophit
@neophit

Here is an example using two delay actions with different durations. I sequenced the removeFromParent action after action2 since it has a longer duration. After two seconds, self will remove itself from its parent.

id action1 = [CCDelayTime actionWithDuration:1.0];
id action2 = [CCDelayTime actionWithDuration:2.0];
id removeFromParent = [CCCallFuncND actionWithTarget:self selector:@selector(removeFromParentAndCleanup:) data:(void*)NO];
id sequence = [CCSequence actionOne:action2 two:removeFromParent];
id spawn = [CCSpawn actionOne:action1 two:sequence];
[self runAction:spawn];

January 14, 2011 at 4:14 am #310414

Benihana
Participant
@benihana

If the actions are of unknown duration, wrap them in a CCSpawn, then wrap the CCSpawn and a CCCallFunc (removeFromParent) in a CCSequence.

January 14, 2011 at 1:10 pm #310415

binarymelon
Participant
@binarymelon

Thanks for the help guys. It worked like a charm. Quick follow up to Benihana’s comment. Wouldn’t that be the best practice in all cases? I can see why there probably wouldn’t be any issues with neophit’s solution, but if you could pause an action independent of the node (does CCSpeed allow this?) wouldn’t that cause an issue?

January 15, 2011 at 1:20 am #310416

Benihana
Participant
@benihana

I don’t see any issues with Neophits approach, except that if you change the duration in the future without realizing the logic needs changing (i.e. action1 duration is 3.0 instead of 1.0) or actions are changed to allow duration to be changed. I don’t quite follow your example, but the point remains.

I either posted right after Neophit, or accidentally skipped over his post. Didn’t mean to step on his toes.

January 15, 2011 at 5:42 pm #310417

neophit
@neophit

I wasn’t sure of CCSpawn’s behavior when wrapped in a CCSequence and I didn’t have time to test before posting so I went with something I knew would work. After testing, I see that CCSpawn’s duration is basically the longest child action’s duration. So wrapping the actions is a CCSpawn is cleaner.

id action1 = [CCDelayTime actionWithDuration:1.0];
id action2 = [CCDelayTime actionWithDuration:2.0];
id spawn = [CCSpawn actionOne:action1 two:action2];
id removeFromParent = [CCCallFuncND actionWithTarget:self selector:@selector(removeFromParentAndCleanup:) data:(void*)NO];
id sequence = [CCSequence actionOne:spawn two:removeFromParent];
[self runAction:sequence];

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.