|Read more about this book|
(For more resources on Cocos2d, see here.)
Handling accelerometer input
Now that we have our three basic elements roughly defined, let’s focus on the player’s interaction with the hero. We will start by allowing the player to move the hero using the device’s built-in accelerometer.
The accelerometer opens a new world of interaction between the user and the device, allowing for a lot of interesting uses.
There are already lots of applications and games that use this feature in innovative ways. For example, in the game Rolando, players have to roll the main character around using the accelerometer, and here we will be doing something similar to that.
The accelerometer gives you data of the current tilting of the device in the three axes, that is the x, y, and z axes. Depending on the game you are making, you will be needing all that data or just the values for one or two of the axis.
Fortunately, Apple has made it very easy for developers to access the data provided by the accelerometer hardware.
Time for action – moving your hero with the accelerometer
We have to add a few lines of code in order to have our hero move. The end result will be the hero moving horizontally when the user tilts the device to the left or to the right. We will also do some checkovers, in order to avoid the hero moving out of the screen.
- The first step involved in using the accelerometer is to enable it for the CCLayers that want to receive its input. Add the following line of code to the GameLayer’s init method:
self.isAccelerometerEnabled = YES;
Then we have to set the update interval for the accelerometer. This will set the interval at which the hardware delivers the data to the GameLayer.
- Add the following line afterwards:
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:
(1.0 / 60)];
Now that our GameLayer is prepared to receive accelerometer input, we just have to implement the method that receives and handles it.
- The following is the method that receives the input and makes our hero move. Add it to the GameLayer class:
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
static float prevX=0, prevY=0;
#define kFilterFactor 0.05f
float accelX = (float) acceleration.x * kFilterFactor + (1-
prevX = accelX;
//If the hero object exists, we use the calculated accelerometer values to move him
//We calculate the speed it will have and constrain it so it doesn't move faster than he is allowed to
float speed = -20 * -accelX;
if(speed > hero.movementSpeed)
speed = hero.movementSpeed;
else if(speed < -hero.movementSpeed)
speed = -hero.movementSpeed;
//We also check that the hero won't go past the borders of the screen, if he would exit the screen we don't move it
if((accelX >0 || hero.mySprite.position.x
>hero.mySprite.textureRect.size.width / 2) && ( accelX <0
hero.mySprite.textureRect.size.width / 2))
- Run the game now. You should be able to move the hero by tilting you device, as shown in the following screenshot:
The iPhone simulator does not provide a way to simulate accelerometer input, so you won’t be able to test it in there; it won’t move at all.
What just happened?
Accelerometer input can be achieved quite easily and fast. In our example, we just used the x component of the accelerometer input to handle the hero’s position.
The accelX variable holds the current value of the x component. This value ranges from -1 to 1, so we multiply it by the speed of the hero, then we just add that result to the current position of the hero, giving the sense of motion.
We are also checking to make sure that the hero is not going off the screen before applying that movement.