Game Development Reference
In-Depth Information
Managing the
UIView
The last improvement to the
GameController
class is the task
updateViewForActor
:, shown in
Listing 6-8.
Listing 6-8. GameController.m (updateViewForActor:)
-(void)updateViewForActor:(Actor*)actor{
NSObject<Representation>* rep = [actor representation];
UIView* actorView = [rep getViewForActor:actor In:self];
[rep updateView:actorView ForActor:actor In:self];
float xFactor = actorsView.frame.size.width/self.gameAreaSize.width;
float yFactor = actorsView.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);
actorView.transform = CGAffineTransformIdentity;
[actorView setFrame:frame];
actorView.transform = CGAffineTransformRotate(actorView.transform, [actor rotation]);
[actorView setAlpha:[actor alpha]];
}
The task
updateViewForActor
: is responsible for managing the
UIView
associated with each actor.
In the first line of this task, we get the actor's representation property and store it in the variable
rep. The variable rep is an
NSObject
that conforms to the protocol Representation. The protocol
Representation
is defined in
Actor.h
(shown later in Listing 6-9) and describes how a
UIView
for a
given actor is created and updated.
UIViews
are created when
getViewForActor:In
: is called. When
a
UIView
is updated to reflect a change to an actor, the task
updateView:ForActor:In
: is called.
These two tasks are discussed later in this chapter.
After the
UIView actorView
is identified, we calculate the region of its parent view that it should take
up. The details of this calculation were described in Chapter 5. We add two new features, however;
we rotate
actorView
based on the actor's rotation property, and we also set the alpha value of
actorView
, allowing actors to change their opacity during a game.
Now that we have looked at the class
GameController
and how it manages the actors in a game,
let's look at the class
Actor
so you can understand how it allows for many different types of actors in
any game.
The Actor Class
The class
Actor
is the super-class for all actors in your game. It primarily provides information about an
actor's location, but it also describes protocols that indicate how an actor behaves and is represented