Book Image

Cocos2d-X Game Development Blueprints

By : Karan Sequeira
Book Image

Cocos2d-X Game Development Blueprints

By: Karan Sequeira

Overview of this book

Table of Contents (17 chapters)
Cocos2d-x Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding tilt controls


Tilt controls, also referred to as the accelerometer, can be added to any layer in the same way touch controls are added. It is as simple as calling the setAccelerometerEnabled(true)function in the init function of GameWorld and then overriding the virtual function didAccelerate. Let's take a look at the didAccelerate function:

void GameWorld::didAccelerate(CCAcceleration* acceleration_value)
{
  HandleInput(ccp(acceleration_value->x, acceleration_value->y));
}

The didAccelerate function gets a parameter of type CCAcceleration*, which contains three values: x, y, and z. These three values signify how much the device has tilted in the x, y, and z directions. Take a look at the following table to understand the values Cocos2d-x passes into the didAccelerate function:

Tilt direction

X

Y

Extreme left

-0.9f to -1.1f

0.0f

Extreme right

0.9f to 1.1f

0.0f

Extreme forward

0.0f

0.9f to 1.1f

Extreme backward

0.0f

-0.9f to -1.1f

Note

The preceding values are approximate...