Book Image

Learning Cocos2d-x Game Development

By : Siddharth Shekar
Book Image

Learning Cocos2d-x Game Development

By: Siddharth Shekar

Overview of this book

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

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. We add the following code to the previous function:

distFraction = visibleSize.height*  pAccelrometer->x;

Create a global variable named distFraction of the float type in HelloWorldScene.h.

pAccelrometer->x is the value of acceleration...