【发布时间】:2014-01-10 01:34:41
【问题描述】:
我目前正在使用 sprite kit 编写游戏,我有 8 种不同的方法,我将其设置为每 5 秒调用 1 个方法,但我希望它随机调用 1 个方法,而不是仅仅能够调用 1 个方法选择 8 个方法中的 1 个并调用它。这是我当前的代码:
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {
self.lastSpawnTimeInterval += timeSinceLast;
if (self.lastSpawnTimeInterval > 5) {
self.lastSpawnTimeInterval = 0;
[self shootPizza];
}
}
- (void)update:(NSTimeInterval)currentTime {
// Handle time delta.
// If we drop below 60fps, we still want everything to move the same distance.
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
timeSinceLast = 1.0 / 60.0;
self.lastUpdateTimeInterval = currentTime;
}
[self updateWithTimeSinceLastUpdate:timeSinceLast];
}
【问题讨论】:
-
为什么不只调用一个方法并传入当前随机数,然后使用 switch(number){} 为每个数字运行代码?
标签: iphone ios7 xcode5 sprite-kit