Cocos2dx: Simulate touch events

Source: Internet
Author: User

Recently I finally began to learn about the cocos2d-x engine, Cocos2d-x as a mobile game engine, the screen touch event (touch) processing must be very important. Last week, I began to figure out how to simulate the touch event through the CCConsole command on the telnet terminal? To simulate touch events in the engine, you must first figure out how the engine handles touch events. In the past, the processing of touch events is complicated. Therefore, you must first understand the code or materials.

Touch events are transmitted to the CPU through touch screen sensors, the CPU then calls the driver's interrupt processing program to the OS, and the OS sends the event to the application for processing through the touch Event Callback interface. The engine is clearly located at the application layer. The target is the OS callback interface. for Android, the app portal is android_main (), and the callback interface is also specified here:

Void android_main (struct android_app * state ){


// Make sure glue isn' t stripped.
App_dummy ();

Memset (& engine, 0, sizeof (engine ));
State-> userData = & engine;
State-> onAppCmd = engine_handle_cmd;
State-> onInputEvent =Engine_handle_input;
State-> inputPollSource. process = process_input;
Engine. app = state;

....

}


The input event processing function is engin_handle_input:


Static int32_t engine_handle_input (struct android_app * app, AInputEvent * event ){


Pthread_t thisthread = pthread_self ();
LOG_EVENTS_DEBUG ("engine_handle_input (% X, % X), pthread_self () = % X", app, event, thisthread );


Struct engine * engine = (struct engine *) app-> userData;


If (AInputEvent_getType (event) = AINPUT_EVENT_TYPE_MOTION ){
Engine-> animating = 1;
Engine-> state. x = AMotionEvent_getX (event, 0 );
Engine-> state. y = AMotionEvent_getY (event, 0 );


ReturnHandle_touch_input(Event );
}
Else
Return handle_key_input (event );


Return 0;
}


There are two types of input events: touch input and key input. We care about touch events. The processing function is handle_touch_input ():


Static int32_t handle_touch_input (AInputEvent * event ){


Pthread_t thisthread = pthread_self ();
LOG_EVENTS_DEBUG ("handle_touch_input (% X), pthread_self () = % X", event, thisthread );


Switch (AMotionEvent_getAction (event )&
AMOTION_EVENT_ACTION_MASK ){


Case AMOTION_EVENT_ACTION_DOWN:
{
LOG_EVENTS_DEBUG ("AMOTION_EVENT_ACTION_DOWN ");
Int pointerId = AMotionEvent_getPointerId (event, 0 );
Float xP = AMotionEvent_getX (event, 0 );
Float yP = AMotionEvent_getY (event, 0 );


LOG_EVENTS_DEBUG ("Event: Action DOWN x = % f y = % f pointerID = % d \ n ",
XP, yP, pointerId );
Float x = xP;
Float y = yP;


Cocos2d: Director: getInstance ()-> getOpenGLView ()-> handleTouchesBegin(1, & pointerId, & x, & y );
Return 1;
}
Break;

.....

}
}

To save space, only the code of touch begin is listed. Others are similar.

In this way, it is clear that to simulate the screen touch event, you only need to pass through:

Cocos2d: Director: getInstance ()-> getOpenGLView ()-> handleTouchesBegin/handleTouchesEnd/handleTouchesMove

These interfaces can be implemented.

In this way, you can enter

> Touch tap 300 600

> Touch swipe 800 600 800 300

To simulate touch events.

The code can be viewed here:

Https://github.com/cocos2d/cocos2d-x/pull/5462


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.