Game Development Reference
In-Depth Information
In Listing 5-7, we see the header file for the class
Actor02
. There are five properties that represent
the basic attributes required for this example. The property
actorId
is used to identify the actor;
we will see how we use this to map each
Actor02
to an
UIImageView
. The property
center
tells us
the location of each
Actor02
. The property
speed
indicates how fast this actor moves. The property
radius
represents the size of this actor. Finally, the property
imageName
describes how this actor
should be drawn on the screen.
The type of the property
center
is
CGPoint
. Strictly speaking, we will not be directly using this
CGPoint
to set the location of a
UIView
or other Core Graphics component. But
CGPoint
is a simple
way to wrap an X and Y value, and we are already familiar with it. For this example, it makes little
difference if we use
CGPoint
or a struct of our creation to represent a point. However, if you wanted
to make sure your code was not dependent on Core Graphics in any way, you would want to define
your own type for representing a point.
Also of note in Listing 5-7 is that an
Actor02
does not have a width or height, just a
radius
. This
is done to simplify the example, and also because many games require only a single dimension
to describe the size of an actor. If you build your own game framework, you may choose to have
your actors include a width and height—it really depends on the game. Certainly if you look at
prebuilt game engines you will find that their base type has all sorts of features you may or may
not use. It may make a lot of sense to use a game engine, but here we are trading complexity for
understanding.
A Simple Actor
Listing 5-7 shows that the
Actor02
has three tasks defined. Listing 5-8 shows the implementation of
these tasks.
Listing 5-8. Actor02.m
#import "Actor02.h"
@implementation Actor02
@synthesize actorId;
@synthesize center;
@synthesize speed;
@synthesize radius;
@synthesize imageName;
-(id)initAt:(CGPoint)aPoint WithRadius:(float)aRadius AndImage:(NSString*)anImageName{
self = [super init];
if (self != nil){
[self setActorId:[NSNumber numberWithLong:nextId++]];
[self setCenter:aPoint];
[self setRadius:aRadius];
[self setImageName:anImageName];
}
return self;
}
-(void)step:(Example02Controller*)controller{
//implemented by subclasses.
}