4
重新启动我的游戏时,它会因内存不足警告而崩溃。 我正在寻找一种方法来关闭EAGLView并停止所有进程。 我不知道该告诉你什么,所以如果需要,请索取更多信息。关闭EAGLView并停止所有进程
我有一个EAGLView与mainGameLoop如下。
- (void)mainGameLoop {
// Create variables to hold the current time and calculated delta
CFTimeInterval time;
float delta;
// This is the heart of the game loop and will keep on looping until it is told otherwise
while(true) {
// Create an autorelease pool which can be used within this tight loop. This is a memory
// leak when using NSString stringWithFormat in the renderScene method. Adding a specific
// autorelease pool stops the memory leak
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// I found this trick on iDevGames.com. The command below pumps events which take place
// such as screen touches etc so they are handled and then runs our code. This means
// that we are always in sync with VBL rather than an NSTimer and VBL being out of sync
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.02, TRUE) == kCFRunLoopRunHandledSource);
// Get the current time and calculate the delta between the lasttime and now
// We multiply the delta by 1000 to give us milliseconds
time = CFAbsoluteTimeGetCurrent();
delta = (time - lastTime) * 1000;
// Go and update the game logic and then render the scene
[self updateScene:delta];
[self renderScene];
// Set the lasttime to the current time ready for the next pass
lastTime = time;
// Release the autorelease pool so that it is drained
[pool release];
}
}
首先,你将需要一种方式来打破你的游戏循环。你有没有想过让你的'while()'条件实际上检查一个值?此外,使用'while()'循环可能不是更新显示的最佳方式。你真的应该考虑使用CADisplayLink。最后,[EAGLView不是Cocoa类的股票](http://stackoverflow.com/a/8438138/19679),它只是一个自定义的UIView,托管了OpenGL ES CAEAGLLayer,有时用于Apple的示例代码中,所以有些人可能不知道那是什么。 – 2012-04-20 14:45:50
该示例是raw mainGameLoop。我认为有人可能有另一种方式来做到这一点。 Atm我正在使用while(self.hidden == FALSE)并在显示得分时隐藏视图等。 我的游戏在while中断了(CFRunLoopRunInMode(kCFRunLoopDefaultMode,0.02,TRUE)== kCFRunLoopRunHandledSource);现在,在几次重播游戏之后。 – Lohardt 2012-04-21 08:59:48
为什么不使用NSTimer或CADisplayLink呢? – Max 2012-04-21 13:47:53