Game Development Reference
In-Depth Information
a
Viper
object and store it in the variable
viper
. Once we are reset, we set
isPaused
to
NO
and inform
the
delegate
that the game has started. Let's take a look at how we handle input and then we will be
ready to move beyond the setup.
Handling Input
As soon as
isPaused
is set to
NO
, the game is up and running. At this point, the only
actor
in the
game is the
Vipe
r. Let's take a moment to understand how we intercept input to make it move before
the Asteroids and aliens show up. The task
tapGesture:
is shown in Listing 12-10.
Listing 12-10. BeltCommanderController.m (tapGesture:)
if (![self isPaused]){
CGSize gameSize = [self gameAreaSize];
CGSize viewSize = [actorsView frame].size;
float xRatio = gameSize.width/viewSize.width;
float yRatio = gameSize.height/viewSize.height;
CGPoint locationInView = [tapRecognizer locationInView:actorsView];
CGPoint pointInGame = CGPointMake(locationInView.x*xRatio, locationInView.y*yRatio);
[viper setMoveToPoint: pointInGame within:self];
}
In Listing 12-10, we see the task
tapGesture:
that is called when the user taps the screen. In this task,
after checking if we are paused, we have to convert the point of the touch into game coordinates.
On the iPhone, this is not strictly necessary because the view
actorsView
is the same number of points
wide and high as the size of our game. However, when we play this game on the iPad, the
actorsView
is 1024x682, while our game is still 640x480. In order to convert from one coordinate space to the
other, we divide the game width by the width of
actorsView
and multiply it by the
X
location of the
touch. We repeat the process for the height and
Y
value. Once we have the point figured out, we
call
setMoveToPoint:within:
on Viper, which sets it in motion. We will look at the implementation of
setMoveToPoint:within:
when we take a closer look at the class Viper in the next section.
We have covered all there is to know about getting
BeltCommanderController
setup to play the
game. Everything is ready to start processing user input and adding new
Actors
to the mix. The
following section describes how
BeltCommanderController
checks for an end condition, adds
Actors
, and performs other task appropriate for the
BeltCommanderController
class.
BeltCommanderController, One Step at a Time
We have our
BeltCommanderController
class all set up and ready to play the game. We are going to
look at the code that is run for every step to manage the overall state of the game. We will look at
how the end condition is tested for, how we add new
Actors
to the scene, and how we manage the
interactions between the
Actors
. Since
BeltCommanderController
is a subclass of
GameController
,
we start out in game logic in the task
applyGameLogic
, as shown in Listing 12-11.