Time for action – reading accelerometer data
Just as you do with touch
events, you need to tell the framework you want to read accelerometer data.
- You tell the framework you wish to use the accelerometer with this one call inside any
Layer
class:Device::setAccelerometerEnabled(true);
- Then, just as you've done with
touch
events, you subscribe to theaccelerometer
events from the event dispatcher as follows:auto listenerAccelerometer = EventListenerAcceleration::create(CC_CALLBACK_2 (GameLayer::onAcceleration, this)); _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerAccelerometer, this);
- In Eskimo, the accelerometer data changes the value of a
Point
vector called_acceleration
.void GameLayer::onAcceleration(Acceleration *acc, Event *event) { _acceleration = Vec2(acc->x * ACCELEROMETER_MULTIPLIER, acc->y * ACCELEROMETER_MULTIPLIER); }
This value is then read inside the main loop and used to move the Eskimo. In the game, only one...