1. Call the cclayer method setAccelerometerEnabled (true) to enable gravity sensing in the layer
2. After gravity sensing is enabled, when the gravity direction changes, the cclayer method didAccelerate (CCAcceleration * pAccelerationValue) will be called back, and this method will be rewritten in the Self-derived layer, such:
[Java]
Void MyCarLayer: didAccelerate (CCAcceleration * pAccelerationValue)
{
// The smaller the speed value to control the deceleration, the easier it is to change the direction.
Float deceleration = 0.5f;
// The greater the sensitivity value of the accelerator, the more sensitive the main character will be to the accelerator's input.
Float sensiti.pdf = 10.0;
// Maximum moving distance
Float maxVelocity = 100366f;
M_myCarMoveDis.x = m_myCarMoveDis.x * deceleration + pAccelerationValue-> x * sensitiation;
If (m_myCarMoveDis.x> = maxVelocity)
{
M_myCarMoveDis.x = maxVelocity;
}
Else if (m_myCarMoveDis.x <=-maxVelocity)
{
M_myCarMoveDis.x =-maxVelocity;
}
}
PAccelerationValue contains the gravity values of x, y, and z (determined by the offset of the mobile phone in these three directions)
Generally, this callback function obtains the offset distance of the gravity sensor protagonist, and adds the distance to the current position of the protagonist in update.