Game Development Reference
In-Depth Information
Picking the positions of the thumbstick or the directional pad is a little more
complex. Usually, you get x and y values and you can work from there:
GCControllerDirectionPadValueChangedHandler dpadMoveHandler =
^(GCControllerDirectionPad *dpad, float xValue, float yValue) {
if (yValue > 0)
{
player.accelerating = YES;
} else {
player.accelerating = NO;
}
};
In this block, we check the y value, and if it is higher then
0
(neutral), we start jumping.
Each controller also has a pause button, and you should implement the functionality
of pausing your game when the user presses the pause button. The controller holds a
block for the pause button that gets triggered each time it is pressed.
Using a controller in our game
As our game is not too complicated, we use only one button. We will map the same
thing to a few buttons so that the players can use any one that they like.
The first thing that we need to do is import the
GameController
framework. In order
to do this, add the following line to
ERGMyScene.h
at the beginning of the file:
@import GameController;
The preceding line of code adds game controller headers and links our project with
the game controller library while it is compiling. After this, we will need two new
properties in the same file:
@property (strong, nonatomic) ERGPlayer *player;
@property (strong, nonatomic) SKLabelNode *pauseLabel;
We will need a way to access our
player
object from the code that sets up the game
controllers, so we add the first property in the preceding code to hold it.
The second one is the label for the paused state of our game. We will show or hide it
based on the current game state.
Right after the line where you add
player
as a child in the
initWithSize:
method
in
ERGMyScene.m
, add the following line of code:
self.player = player;