[The Path to COCOS-2D programming for a cainiao] preliminary use of the UIAccelerometer in COCOS2D

Source: Internet
Author: User

UIAccelerometer, in short, is to obtain the gravity sensing and displacement judgment and processing of the mobile phone. For example, shaking the mobile phone up or down will trigger the accelerator event. Today, we will learn how to use the accelerator.

1. Code. The implementation of the accelerator is very simple, as long as the only method in the proxy is implemented.

UIAccelerometerDelegate


The proxy method is

-(Void) accelerometer :( UIAccelerometer *) accelerometer didAccelerate :( UIAcceleration *) acceleration NS_DEPRECATED_IOS (2_0, 5_0)

Acceleration encapsulates the xyz coordinates of the accelerator. We can operate our Genie Based on xyz changes.


1-) the simplest assignment of method1

   UIAccelerationValue x,y,z;        x = acceleration.x;    y = acceleration.y;    z = acceleration.z;

We can perform corresponding processing based on the obtained xyz value. But this xyz value contains gravity, our acceleration event. Resolution later


2) obtain the part affected by gravity. Use the low-pass Filtering Algorithm

/// # Define kAccelerometerFrequency 50.0 // # define kFilteringFactor 0.1 // obtain the UIAccelerationValue accleX, accleY, accleZ; accleX = (acceleration. x * kFilteringFactor) + (accleX * (1.0-kFilteringFactor); accleY = (acceleration. y * kFilteringFactor) + (accleY * (1.0-kFilteringFactor); accleZ = (acceleration. z * kFilteringFactor) + (accleZ * (1.0-kFilteringFactor ));

3) Remove the parts affected by gravity from x to obtain the influence of the final xyz accelerator

// Filter the low-pass data out of the current value to obtain the simplified high-pass data UIAccelerationValue haccleX, haccleY, haccleZ; haccleX = x-accleX; haccleY = y-accleY; haccleZ = z-accleZ; // use your own method to process data self. isTouchEnabled = YES;

Note that we need to set a touch response.


Self. isTouchEnabled = YES;


Related Article

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.