iPhone Cookbook - Coding recipes for iPhone Developers
Code examples for doing simple and interesting things on the iPhone and iPod touch.
Monday, October 19, 2009
Just being random.
arc4random();
to set a start and range:
#define RANDOM_START 10
#define RANDOM_RANGE 20
#define randomNumber() ((arc4random() % RANDOM_RANGE) + RANDOM_START)
this will give random numbers from 10 through 29. 20 numbers starting at 10.
OR
#define randomNumber(s, r) (arc4random() % (r) +(s))
Note that a range of 5 will give 5 numbers, not 0 - 5 but 0 - 4
Good Vibrations
AudioServicesPlaySystemSound
(kSystemSoundID_Vibrate);
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];
}