iPhone Game Development - Chris Craft [30]
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
placardView.center = self.center;
placardView.transform = CGAffineTransformIdentity;
}
Notice that we are simply setting the placard back to its original state with no glitz or animations. The player will probably not even see this happen, so keep it short and sweet.
Envisioning Animations
Like Multi-Touch, animations add an element of excitement and immersion to a player's overall gaming experience. Animations are not new to gamers; in fact, we would argue that in today's gaming market, this is a minimal expectation. However, high-quality games with smooth animations were not generally seen on a hand-held phone. This is yet another quality of the iPhone that sets it apart from other mobile devices.
Luckily for you, Apple did not stop with making high-quality animations possible. High-quality animations are easy to achieve without having to code every frame of the process. Let's revisit the previous example but this time focus on animations.
Earlier you saw the code for the method touchesBegan. In touchesBegan you will see a call to the method animateFirstTouchAtPoint. This method is responsible for the animation you see when you first touch the placard. Here's a look at the source to this method:
- (void)animateFirstTouchAtPoint:(CGPoint)touchPoint {
NSValue *touchPointValue = [[NSValue valueWithCGPoint:touchPoint] retain];
[UIView beginAnimations:nil context:touchPointValue];
[UIView setAnimationDuration:0.15];
CGAffineTransform transform =
CGAffineTransformMakeScale(1.2, 1.2);
[UIView setAnimationDidStopSelector:
@selector(growAnimationDidStop:finished:context:)];
placardView.transform = transform;
[UIView commitAnimations];
}
The previous block of code is the complete animateFirstTouchAtPoint method. In this method you will see that the first line sets the touchPointValue by converting the parameter touchPoint to an NSValue. The touchPointValue is passed as the context parameter of the call to beginAnimations:context:. Ultimately, the context parameter will be passed the method growAnimationDidStop, which is assigned as the selector parameter of the method setAnimationDidStopSelector:. Now the growAnimationDidStop method will be called when the animation stops and touchPointValue will be passed in as the context parameter.
The following code is a further breakdown of the animation steps. First, you will set up the animation by calling these methods:
[UIView beginAnimations:nil context:touchPointValue];
[UIView setAnimationDuration:0.15];
[UIView setAnimationDidStopSelector:
@selector(growAnimationDidStop:finished:context:)];
The first line above begins the animation and passes in the context. The second line tells the animation how long it should last. The third line assigns a selector to be called when the animation is finished.
Now you are ready to tell the view how to animate:
CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
placardView.transform = transform;
In the previous section of code, a call to CGAffineTransformMakeScale is made to create a transform. The transform will scale the placard up by 20 percent:
[UIView commitAnimations];
Finally, the previous line of code is responsible for committing the animation. This is all you have to do! When you call commitAnimations, the animation engine calculates how many frames are necessary and plays them one by one until the specified transform has been performed. In this example, once this is complete, the message growAnimationDidStop is sent.
Programming: AmuckSlider
Now we are going to review our first game with you. We have used Multi-Touch and animations to create the simple puzzle application that we call AmuckSlider. With each of the applications in this book,