Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocos2d schedule only once

How can I make cocos2d schedule selector only once? Right now the best I can do is:

[self schedule:@selector(eventHappend:) interval:2];

and eventHappend:

- (void)eventHappend: (ccTime) dt
{
    [self unschedule:@selector(eventHappend:)];
    // Do stuff
}

but that seems like a workaround... is there a method to schedule only once?

like image 376
Dani Avatar asked May 27 '11 19:05

Dani


2 Answers

You can run a sequence of actions on the node, a CCDelay followed by a CCCallFunc that invokes your method.

Like this...

[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:2], 
                                    [CCCallFunc actionWithTarget:self selector:@selector(eventHappened)],
                                    nil]];
like image 78
Fraser Graham Avatar answered Nov 13 '22 06:11

Fraser Graham


- (void) scheduleOnce: (SEL)selector delay: (ccTime)delay 

Schedules a selector that runs only once, with a delay of 0 or larger

http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#afe99d609f17c4c849e4543805ffeceab

like image 44
Mark Avatar answered Nov 13 '22 06:11

Mark