Hi, I am looking for example about scheduler, anyone could help me?
examples about scheduler
(8 posts) (3 voices)-
Posted 2 years ago #
-
I'm using it like this:
//function
-(void)tick:(ccTime)dt
{
//your code goes here
// example ivar+=1; blah blah}
Then call the scheduler like this:
[self schedule: @selector(tick:) interval:2]; //specify interval in seconds
Hope this helps
Posted 2 years ago # -
have you looked at the demos in the main cocos2d download? Theres plenty that make use of schedules. Artem shows you the base bits above otherwise.
Posted 2 years ago # -
thanks guys, that are helpful.
but I have another question, how can I stop the scheduler under specific condition?
for example,- (void)createEnemy:(ccTime)dt {
//do some stuff
}
and then,[self schedule:@selector(createEnemy:) interval: 1] // create an enemy every second
so, suppose I just need 5 enemies in all, what can I do to stop creating?
Posted 2 years ago # -
I am waiting for your help
Posted 2 years ago # -
I find a way. I add a counter in createEnemy and put this schedule into the main loop, so I can do a test in the main loop. code like this:
static int counter=0;
-(void)createEnemy;(ccTime)dt {
//do some stuff
counter++;
}// main loop
if(counter < 5) {
[self schedule:@selector(createEnemy:) interval: 1];
}
else {[self unschedule:@selector(createEnemy:)];}it does work for me, is this OK?
Posted 2 years ago # -
I think that means you re-scheduling the selector every run of your main loop. You only need it to run once.
You could do it...
if (counter == 0) {
start schedule...
}
else if (counter == 5) {
stop schedule...
}Or, probably a better way, would be to trigger the schedule in your init or some other run once method, and check for the value of counter within your createEnemy method, and cancel it there if its 5 or whatever. This keeps your main loop less cluttered and processing less code each frame.
Posted 2 years ago # -
if (counter == 0) {
start schedule...
}
else if (counter == 5) {
stop schedule...
}
I tried to put the test like this way, it did not work, so I add another schedule in initPosted 2 years ago #
Reply
You must log in to post.