OpenGL ES 在随机延迟后执行选择器
我正在开发一个 OpenGL ES 应用程序,其中有一艘装有 6 门枪的宇宙飞船。每把枪都使用关键帧动画在起始位置和结束位置的 2 组顶点之间进行插值。
我有一个方法rotateGun:
,我将gunNumber变量传递给它来指代哪把枪应该开火。当 rotateGun:
触发时,它会产生激光爆炸,当调用该方法时,我会通过指向枪管所在位置的矢量将激光爆炸从飞船上移开。这一切都工作正常,但我想为每把枪的开火添加随机时间间隔,因为现在它们似乎同时开火。
我尝试使用performSelector:afterDelay:创建时间延迟并触发rotateGun:方法,但这不起作用。然后,我尝试在延迟后使用方法 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 ...
}
}
I am working on an OpenGL ES application where I have a spaceship with 6 guns. Each gun uses keyframe animation to interpolate between 2 sets of vertices of a start and end position.
I have a method rotateGun:
that I pass the gunNumber variable to that refers to which gun should fire. When rotateGun:
fires, it creates a laser blast that I move away from the ship through a vector pointing down the gun barrel at it's location when the method is called. This all works fine but I want to add random time intervals for each gun to fire since right now they appear to fire at the same time.
I've tried to create a time delay and fire off my rotateGun: method using performSelector:afterDelay:
, but this doesn't work. I then attempted to use the method mainGunFire:
after a delay and then call rotateGun:
on the main thread… this also didn't work. By "didn't work" I mean that the NSLog I inserted just before my drawing calls within the rotateGun:
method does print, but the guns and blasts are never drawn.
If I simply do a performSelectorOnMainThread to call rotateGun:
, then the guns and blasts are drawn as before and the blasts appear to fire at the same time. I'm clearly not understanding something. Can someone please help me understand how to slightly randomize my laser blasts so they don't all fire at the same time? Thanks!
// 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 ...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案是使用
!(rand()%some_number)
而不是 1。尝试使用some_number
值(每把枪的值必须不同)。但应该不会很大。例如,如果您使用 2,则
!(rand()%2) == 1
的概率约为 0.5。因此,如果每秒渲染 60 帧,每秒大约会产生 30 个火焰。对于 !(rand()%20) 你应该每秒得到大约 3 次火灾。希望你能明白。一些伪代码:The easiest solution is to use
!(rand()%some_number)
instead of 1. Experiment withsome_number
value (it must be different for each gun). But it should be not very big.For example if you use 2, then probability of
!(rand()%2) == 1
is about 0.5. So if you're rendering 60 frames per second you'll get about 30 fires per second. For the !(rand()%20) you should get about 3 fires per second. Hope you get it. Some pseudocode: