Wednesday, March 11, 2009

Pausing during calls

I realised yesterday (actually my wife did) that receiving a call during game play of WordSalad doesn't work how it should. The timer keeps ticking away but you are unable to provide any input.

Input is withheld from the UIView's when an event happens (ie receiving an SMS, a phone call or a calendar reminder), but the app does keep running. So it turns out its pretty simple what needs to be done. The application delegate needs to respond to three events:


-(void)applicationWillResignActive:(UIApplication *)application {
[[self rootViewController] pauseGame];
}

-(void)applicationWillTerminate:(UIApplication *)application {
[[self rootViewController] saveSettings];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
[[self rootViewController] unpauseGame];
}

The sequence of events is easy to understand.
  1. Application starts
  2. Interruption occurs (phone/sms/calendar), applicationWillResignActive is called
  3. 2 options here. Either the user:
  • a) Rejects the call/sms/calendar event, and applicationDidBecomeActive is called
  • b) Accepts the call/sms/calendar event, and applicationWillTerminate is called
Now, as it happens, I already had the applicationWillTerminate: code in place, as this is where your application should save its settings to allow for continuing where the user left off. So really I just needed some code to pause and unpause the game. This was very simple. Rather than mess around killing timers and then restarting them, I simply made the timer return early if the game was 'paused'. Since input is not getting passed to the app, as long as nothing moves (including the timer) the game appears paused.

Unpausing was simply a matter of setting gamePaused = NO. Too easy!

Of course, WordSalad is a simple game, so there is almost nothing happening in the background, apart from the timer, it is purely event driven. Game loops probably shouldn't run full speed during an interruption... there is no point wasting that power if the user cannot actually interact.


2 comments:

  1. Thanks
    Nice code.
    Can I create this type of effect on UIView
    check this link
    http://www.flickr.com/photos/24313234@N07/5323036634/


    please write the code for that

    ReplyDelete
  2. All you need to do is modify drawRect() to draw the appropriate shape, although, something like that link is possibly just an image with transparency.

    ReplyDelete