Code examples for doing simple and interesting things on the iPhone and iPod touch.

Tuesday, September 29, 2009

exit by animation

In games for the iPhone, processing level to level can be tricky

Here is a way to leave the level - in this case a bonus level.

The playing screen - attached to a UIView called: backgroundView is faded and then removed.

This is nice because the fading looks good, its OS supported and releases managed views automatically.



// called by the animation thread when animation is finished

- (void)bonusExitFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context

{

NSLog(@"Bonus Exit Finished");


// the subviews are all released automatically when backgroundView removes itself

[backgroundView removeFromSuperview];


// use a delegate to tell the owner that this round is finished

[delegate roundFinished];

}




// call this at the end of the level: [self exitBonus];

// backgroundView is the UIView that contains all of the subviews


- (void) exitBonus

{

NSLog(@"Bonus Exit");


//pause / slowly fade the screen

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration: 1.8f];

[backgroundView setAlpha: 0.0f];

[UIView setAnimationDelegate: self];

[UIView setAnimationDidStopSelector: @selector(bonusExitFinished:finished:context:)];

[UIView commitAnimations];

}



Followers