2012-04-20 84 views
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]; 
    } 
} 
+0

首先,你將需要一種方式來打破你的遊戲循環。你有沒有想過讓你的'while()'條件實際上檢查一個值?此外,使用'while()'循環可能不是更新顯示的最佳方式。你真的應該考慮使用CADisplayLink。最後,[EAGLView不是Cocoa類的股票](http://stackoverflow.com/a/8438138/19679),它只是一個自定義的UIView,託管了OpenGL ES CAEAGLLayer,有時用於Apple的示例代碼中,所以有些人可能不知道那是什麼。 – 2012-04-20 14:45:50

+0

該示例是raw mainGameLoop。我認爲有人可能有另一種方式來做到這一點。 Atm我正在使用while(self.hidden == FALSE)並在顯示得分時隱藏視圖等。 我的遊戲在while中斷了(CFRunLoopRunInMode(kCFRunLoopDefaultMode,0.02,TRUE)== kCFRunLoopRunHandledSource);現在,在幾次重播遊戲之後。 – Lohardt 2012-04-21 08:59:48

+1

爲什麼不使用NSTimer或CADisplayLink呢? – Max 2012-04-21 13:47:53

回答

0

我已經發布了一款遊戲,並且我使用了EAGLView。我還使用了一個計時器來控制幀數/秒(也被稱爲控制設備本身功耗的好方法)。

最好的方法是識別遊戲退出時的所有實例(甚至是按下home按鈕)並控制退出例程的每一位。如果執行上面的定時器,你可以停止計時器,dealloc的東西,等等...

在重新進入,你給API的例程,如果你實現本質上讓你有機會知道你是從遊戲內狀態重新進入,而您的應用程序將從頭開始重新啓動。