Movement with the accelerometer

Just as we enabled touch and then created the function to handle the touches, we will enable the accelerometer and add a function that will get the accelerometer's information and pass it on to this layer.

So, we add the following code to enable the accelerometer:

this->setTouchEnabled(true);

this->setAccelerometerEnabled(true);    

Next, we need to add the following function in the HelloWorldScene.h file:

virtual void didAccelerate(CCAcceleration* pAccelerationValue);

Then, we add the following code snippet in the HelloWorldScene.cpp file, right under the CCTouchesEnded() function:

void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue) 
{

}

Let's now add some code in this function to get data from the accelerometer. ...

Get Learning Cocos2d-x Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.