Game Development Reference
In-Depth Information
Looking at the task step in Listing 5-12, we see that we added a new section at the beginning that
updates which image should be displayed. Every other frame, we increase the value of imageNumber
by one, resetting back to one when it exceeds the constant NUMBER_OF_IMAGES . Each time we
change imageNumber, we want to set needsImageUpdated to YES; otherwise we set it to NO . Let's take
a look at the task updateActorView: and see how Example03Controller uses this information to
make sure the correct image is displayed. See Listing 5-22.
Listing 5-22. Example03Controller.m (updateActorView:)
-(void)updateActorView:(Actor03*)actor{
UIImageView* imageView = [actorViews objectForKey:[actor actorId]];
if (imageView == nil){
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:[actor imageName]]];
[actorViews setObject:imageView forKey:[actor actorId]];
[imageView setFrame:CGRectMake(0, 0, 0, 0)];
[actorView addSubview:imageView];
} else {
if ([actor needsImageUpdated]){
[imageView setImage:[UIImage imageNamed:[actor imageName]]];
}
}
float xFactor = actorView.frame.size.width/self.gameAreaSize.width;
float yFactor = actorView.frame.size.height/self.gameAreaSize.height;
float x = (actor.center.x-actor.radius)*xFactor;
float y = (actor.center.y-actor.radius)*yFactor;
float width = actor.radius*xFactor*2;
float height = actor.radius*yFactor*2;
CGRect frame = CGRectMake(x, y, width, height);
imageView.transform = CGAffineTransformIdentity;
[imageView setFrame:frame];
imageView.transform = CGAffineTransformRotate(imageView.transform, [actor rotation]);
}
In Listing 5-22, we update the image of the imageView if the property needsImageUpdated is set to YES
by simply calling imageName on the actor again. That's all it takes to make the asteroids appear to roll
through space.
The Rotating Effect
Now that we have a way to indicate that the image of an actor should be updated, we can use that
same feature to give some life to the spaceship. At the bottom of Listing 5-22, we can see that the
transformation of the imageView is first modified to an identify transformation, then its frame is set,
and finally the transform is modified again, applying the rotation.
 
Search WWH ::




Custom Search