Using accelerometer in cocos2d-x v3.2

Here is another one of the significant changes in version 3.2 if you are looking forward to use accelerometer in the future. In the init() method of your game Layer add the following code:

auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(GameLayer::onAcceleration, this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

Add the following method which will be invoked by the event listener in the code above.

void GameLayer::onAcceleration(cocos2d::Acceleration *acc, cocos2d::Event *event){
    log("X: %f", acc->x);
    log("Y: %f", acc->y);
    log("Z: %f", acc->z);

And thats it, we are done!
Posted in Tutorials Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *


Join 1 other subscriber