Skip to content

Commit 05b9385

Browse files
committed
Always stop animating when app is backgrounded
Even if the CCDirector isn't the "visible" view controller, the app will still crash if it gets backgrounded and the CCDirector keeps trying to draw. Make sure CCDirector stops drawing if, say, the GameCenter login screen is showing and the user switches away from the app to look up their password in 1Password or something.
1 parent f52a391 commit 05b9385

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

cocos2d/Platforms/iOS/CCAppDelegate.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,27 +268,30 @@ - (void) setupCocos2dWithOptions:(NSDictionary*)config
268268
// getting a call, pause the game
269269
-(void) applicationWillResignActive:(UIApplication *)application
270270
{
271-
if( [navController_ visibleViewController] == [CCDirector sharedDirector] )
271+
if([CCDirector sharedDirector].paused == NO) {
272272
[[CCDirector sharedDirector] pause];
273+
}
273274
}
274275

275276
// call got rejected
276277
-(void) applicationDidBecomeActive:(UIApplication *)application
277278
{
278279
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
279-
if( [navController_ visibleViewController] == [CCDirector sharedDirector] )
280+
if([CCDirector sharedDirector].paused) {
280281
[[CCDirector sharedDirector] resume];
282+
}
281283
}
282284

283285
-(void) applicationDidEnterBackground:(UIApplication*)application
284286
{
285-
if( [navController_ visibleViewController] == [CCDirector sharedDirector] )
287+
if([CCDirector sharedDirector].animating) {
286288
[[CCDirector sharedDirector] stopAnimation];
289+
}
287290
}
288291

289292
-(void) applicationWillEnterForeground:(UIApplication*)application
290293
{
291-
if( [navController_ visibleViewController] == [CCDirector sharedDirector] )
294+
if([CCDirector sharedDirector].animating == NO)
292295
[[CCDirector sharedDirector] startAnimation];
293296
}
294297

0 commit comments

Comments
 (0)