Game Development Reference
In-Depth Information
There are two ways to receive data from the controller, which are as follows:
By reading the properties directly
By assigning a handler (block) to each button to be executed on button press
The following is a sample method to read inputs if you want to read them at
arbitrary times:
- (void) readControlInputs
{
GCGamepad *gamepad = self.myController.gamepad;
if (gamepad.buttonA.isPressed)
[self jumpCharacter];
if (gamepad.buttonB.isPressed)
[self shootApple];
[self moveBy: profile.leftThumbstick.xAxis.value];
}
Here we are checking if the buttons are pressed, and if they are, we execute some
methods such as jumping or shooting. On the last line, we read the value of the
x axis on the left thumbstick to move our character left or right.
The second way to set handlers is as follows:
- (void) setupController
{
GCExtendedGamepad *gamepad = self.myController.extendedGamepad;
gamepad.buttonA.valueChangedHandler = ^(GCControllerButtonInput
*button, float value, BOOL pressed)
{
if (pressed)
[self shoot];
};
gamepad.buttonX.valueChangedHandler = ^(GCControllerButtonInput
*button, float value, BOOL pressed)
{
if (pressed)
[self openInventory];
};
}
What we do here is set the block to each button (in this case, two buttons) to execute
a piece of code each time a button is pressed. One button shoots and the second
button opens the inventory. These handlers also get called when the player releases
the button, so you should check for the pressed variable.
 
Search WWH ::




Custom Search