我正在开发一个 OpenGL ES 应用程序,我有一艘带有 6 支枪的宇宙飞船。每把枪都使用关键帧动画在开始和结束位置的 2 组顶点之间进行插值。
我有一个方法rotateGun:
,我将 gunNumber 变量传递给表示应该开火的枪。开火时rotateGun:
,它会产生激光爆炸,当调用该方法时,我通过指向枪管位置的矢量将其从飞船上移开。这一切都很好,但我想为每把枪的开火添加随机时间间隔,因为现在它们似乎同时开火。
我尝试使用 来创建时间延迟并启动我的 rotateGun: 方法performSelector:afterDelay:
,但这不起作用。然后我尝试mainGunFire:
在延迟后使用该方法,然后调用rotateGun:
主线程……这也不起作用。“不起作用”是指我在rotateGun:
方法中的绘图调用之前插入的 NSLog 确实打印了,但从未绘制过枪声和爆炸声。
如果我只是简单地执行 performSelectorOnMainThread 来调用rotateGun:
,那么枪和爆炸会像以前一样被绘制,并且爆炸似乎同时开火。我显然不明白一些事情。有人可以帮我理解如何稍微随机化我的激光爆炸,这样它们就不会同时发射吗?谢谢!
// Randomly Fire Gun
- (void)mainGunFire:(NSNumber *)gunNumber {
// Perform the Gun animation and fire on main thread
[self performSelectorOnMainThread:@selector(rotateGun:) withObject:gunNumber waitUntilDone:YES];
}
// Draw and Rotate the Guns
- (void)drawRotateGuns {
// Only shoot if this is not a ship life
if ( self.isLife == NO ) {
// Gun 1
// Set the pass variable to 1 and call the method
// after a variable amount of time
int randomTime = 0;
//[self performSelector:@selector(mainGunFire:) withObject:[NSNumber numberWithInt:1] afterDelay:randomTime];
[self performSelectorOnMainThread:@selector(rotateGun:) withObject:[NSNumber numberWithInt:1] waitUntilDone:YES];
// Gun 2 ...
// Gun 3 ...
}
}