如何设置运行方法的次数? (cocos2d iPhone)

时间:2022-06-29 23:58:29

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?

我有一个添加敌人的方法,我想知道我是如何做到的,所以我运行它一定次数(比如10)。我用cocos2d中的调度程序调用该方法并执行[self addEnemy];需要更多信息?

1 个解决方案

#1


1  

If the selector you schedule is methodA:

如果您安排的选择器是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.

如果您愿意,可以在班上使用ivar来跟踪重复次数。

#1


1  

If the selector you schedule is methodA:

如果您安排的选择器是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.

如果您愿意,可以在班上使用ivar来跟踪重复次数。