[英]How to run a method after a boolean change in xcode
因此,我有一个称为- (void)spawnNode
,它基本上可以调节Node的生成。 我的问题是我想这样做,以便当BOOL runGame
等于YES
,spawnNode方法将开始并在一定时间间隔后继续生成我的节点。 我不知道该怎么办。
BOOL runGame
最初设置为NO
。
下面是完整的spawnNode方法:
- (void)spawnNode{
//Spawn Interval Duration
spawnIntervalDuration = 0.75;
//SKAction spawnInterval
spawnInterval = [SKAction waitForDuration:spawnIntervalDuration];
if(runGame){
[self runAction:spawnInterval completion:^{
[self generateNode];
}];
}
}
那么,当runGame
等于YES
,如何使该方法始终运行?
您可以创建一个属性:
@property (nonatomic, assign) BOOL runGame;
另外,还有一个NSTimer属性:
@property (nonatomic) NSTimer *timer;
然后,您需要为其覆盖设置器:
- (void)setRunGame:(BOOL)newValue
{
if (newValue == YES) {
[self spawnNode];
CGFloat spawnInterval = 0.75;
self.timer = [NSTimer scheduledTimerWithTimeInterval:spawnInterval
target:self
selector:@selector(spawnNode)
userInfo:kNilOptions
repeats:YES];
} else {
[self.timer invalidate];
self.timer = nil;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.