Using the acceleration sensor
By using an acceleration sensor on the device, we can make the game more engrossing, by using operations such as shaking and tilting the device. For example, move the ball by tilting the screen, the maze game that aims at the goal, and the skinny panda trying to go on a diet, wherein the players shake the device to play the game. You can get the tilt value and the moving speed of the device by using the accelerometer. If you can use it, your game becomes more unique. In this recipe, we learn how to use the acceleration sensor.
How to do it...
You can get the x, y, and z axis values from the acceleration sensor by using the following code:
Device::setAccelerometerEnabled(true); auto listener = EventListenerAcceleration::create([](Acceleration* acc, Event* event){ CCLOG("x=%f, y=%f, z=%f", acc->x, acc->y, acc->z); }); this->getEventDispatcher()- >addEventListenerWithSceneGraphPriority(listener, this);
How it works...
Firstly, you enable the...