Cocos2d-x event distribution mechanism-accelerometer event monitoring, cocos2d-x Accelerometer

Source: Internet
Author: User

Cocos2d-x event distribution mechanism-accelerometer event monitoring, cocos2d-x Accelerometer

Event monitoring mechanism

In the previous article introduced the cocos2d-x touch event mechanism, this to introduce the next game is also often used in the accelerator events, these are the game is often used.

An important input source on a mobile device is the device direction. Most devices are equipped with an accelerometer to measure the gravity direction of the device when it is still or at a constant speed.

Gravity sensing comes from a mobile accelerator. It usually supports acceleration in the X, Y, and Z directions. It is also called a three-way accelerator. In actual application, the angle and direction of the mobile phone tilt can be calculated based on the strength of the three directions.

In the 3.0 mechanism, we only need to create an accelerometer listener EventListenerAcceleration to implement the callback function of the response and implement the corresponding game logic in the callback function, finally, register the listener to the event distributor _ eventDispatcher.

The static create method of EventListenerAccerlation has an Accerlation parameter. This Accerlation is a class that contains the acceleration in three directions obtained by the accelerator. The Code is as follows:

class Acceleration{public:    double x;    double y;    double z;     double timestamp;     Acceleration(): x(0), y(0), z(0), timestamp(0) {}};
The acceleration of each direction in this class is one gravity acceleration.

Before using the accelerometer event, you need to enable the hardware device first. I didn't notice this before using it. I said why I got it done, the running results are not what I imagined. I finally found that the hardware device was not enabled. Sometimes a small problem may affect you for a long time. In the end, you found that it was a TMD error, I feel like I am about to crash in an instant.

To enable the hardware device, run the following command:

Create a listener and callback function. When creating a callback function, you can use a lambda expression to create an anonymous function or bind an existing function.

Next let's take a look at the complete step code, using an anonymous function:

// Register the Device with gravity sensor: setAccelerometerEnabled (true); // enable the hardware Device auto listener = EventListenerAcceleration: create ([=] (Acceleration * acc, Event * event) {auto ptNow = ball-> getPosition (); // obtain the position of the spirit ball at the moment if (ptNow. x <ball-> getContentSize (). width/2) {ball-> setPositionX (VisibleRect: right (). x); // when the left boundary is exceeded, it is set to exit from the right boundary.} if (ptNow. x> VisibleRect: right (). x-ball-> getContentSize (). width/2) {ball-> setPositionX (VisibleRect: left (). x);} ball-> getPhysicsBody ()-> setVelocity (Vect (acc-> x) * 26000000f, ball-> getPhysicsBody ()-> getVelocity (). y); // obtain the physical properties of the spirit ball and set the speed in the x direction based on the acceleration value acc-> X. The speed in the Y direction remains unchanged }); _ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener, ball); // register the listener

Both the accelerometer mechanism and the touch mechanism are frequently used in mobile games. mastering this knowledge can greatly improve efficiency in development and make the Code look clearer.

Well, this article introduces the mechanisms and implementation methods of the accelerometer listener event. The next article introduces an essential mechanism in another game, the physical collision mechanism in the game, and collision detection.

Cocos2d-x screen Click Event

You need to reload the virtual void registerWithTouchDispatcher (); Function
Director: getInstance ()-> getTouchDispatcher ()-> addTargetedDelegate (this, Menu: HANDLER_PRIORITY-1, true); I am 3.0
2. x is
CCDirector * pDirector = CCDirector: sharedDirector (); pDirector-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );

The cocos2d-x has two layers layer1 and layer2 on the helloworld layer, both of which set tableview to listen to the two layers,

You place the two layers in a different position, give them a name, and use the name to get them...

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: 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.