How do I set how many times to run a method? (cocos2d iPhone)
I have a method to add an 开发者_开发知识库enemy, and I want to know how I can make it so I run it a certain number of times (say 10). I call the method with a scheduler in cocos2d and by doing [self addEnemy]; Need any more info?
If the selector you schedule is methodA
:
[self schedule:@selector(methodA:) interval:1/60];
then a simple way to do what you are looking for is:
- (void) methodA:(ccTime)adelta {
static int counter = 10;
if (--counter >= 0) {
//-- do your processing
} else {
counter = 10; //-- this in case you want to reschedule the method at some later point
[self unschedule:@selector(methodA:)];
}
}
If you prefer it, you could use an ivar in your class to track the number of repetitions.
精彩评论