Game Development Reference
In-Depth Information
One of the goals of this section is to explain how the game logic can be separated from how the
game is drawn on the screen. In the first example, we simply extended
UIImageView
and added
our logic about the spaceship. This worked well enough, but there are lots of good reasons for
separating the logic of the game from the how it is displayed. For starters, if you want to run this
game on an iPad instead of an iPhone, you probably want to make the play area bigger. Also, to
support things like zooming into the game or showing a mini map, it makes sense to have the
location of each item in the game stored in a display-agnostic way. Down the road, you may decide
you want to port your game to another platform. Having a clean separation of game logic from
display logic will save you a lot of work in such a case.
To support this abstraction between the game logic and the display logic we introduce a new
class called
Actor02
. Normally this class would simply be called
Actor
. The 02 in the name simply
indicates it is part of the second example in this chapter. The following section explains what an
sprite
can also
Example2Controller Overview
In our example, we will define the class
Actor02
and show how we can subclass
Actor02
to create our
game items, the spaceship and the asteroids. Listing 5-7 shows the header file for the class
Actor02
.
Listing 5-7. Actor02.h
#import <Foundation/Foundation.h>
@class Example02Controller;
long nextId;
@interface Actor02 : NSObject {
}
@property (nonatomic, retain) NSNumber* actorId;
@property (nonatomic) CGPoint center;
@property (nonatomic) float speed;
@property (nonatomic) float radius;
@property (nonatomic, retain) NSString* imageName;
-(id)initAt:(CGPoint)aPoint WithRadius:(float)aRadius AndImage:(NSString*)anImageName;
-(void)step:(Example02Controller*)controller;
-(BOOL)overlapsWith: (Actor02*) actor;
@end