Cocos2d-x gravity sensing

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.