Ogengine learning notes-event delivery

Source: Internet
Author: User

Ogengine learning notes-event delivery

Event Transfer

There are many events in Android, such as ontouch, onclick, and onlongclick. action_down, action_move, and action_up are shown on a micro-level.

Regardless of the event type, it is first based on the event transfer model. In fact, the event Transfer Model in Android is similar to that in Js. It is based on the form of capture first and then bubble.

In the ogengine API, ogengine is responsible for capturing touch events. If we need to listen to touch events, we need to register with scene.

  1. Mscene. registertoucharea (toucharea );

Copy code


Found by checking the source code of itoucharea

  1. Public Boolean contains (final float PX, final float Py );


  2. Public float [] convertscenetolocalcoordinates (final float PX, final float Py );


  3. Public float [] convertlocaltoscenecoordinates (final float PX, final float Py );


  4. Public Boolean onareatouched (final touchevent pscenetouchevent, final float ptoucharealocalx, final float ptoucharealocaly );


Copy code


In an area, you can determine whether the coordinates of the current touch belong to your own interests. If not, you can discard them directly.
Because the shape class implements the itoucharea interface, our Sprite and other sprite classes can be directly registered.
The relevant code can be found in the scene source code, so we understand that in andengine, listening to touch and entity are not the same concept, and they are independent, this explains why an entity is detach in scene, and the touch in the place where the listener is registered will still trigger the same effect as before.

We will pay attention to the transfer of events. In the source code, we find that because the event requires the area itself to determine whether it belongs to the area, there is no parent-child relationship similar to Android during registration, the transfer is performed one by one according to the registration order. However, if an area declares that it has processed the time (returns true), the transfer sequence will be terminated directly, the event is the same as the event in Android.
However, there will be some problems with such a fixed order. In fact, the designer has considered this issue, so we found a flag in the source code: monareatouchtraversalbacktofront. This flag determines whether the current event is passed in the forward or backward order, which solves our problem to a certain extent.


Screen events

Onareatouchlistener Registration

  1. Basegameactivity. onloadengine (): generate an engine and set engine parameters.

  2. Basegameactivity. startscene (): jump to a scenario

Copy code

Setontouchlistener () is called and the engine is registered to scene as the touchlistener. All subsequent screen events will be aware of calling the ontouch () method of the engine.


Preprocessing of screen events


The screen events in Android are instant bursts. That is to say, as long as a screen event is generated, the ontouch () method of the listener registration will be directly called in the event thread. If the processing time is too long, android directly judges the program as unresponsive and is kicked out. Two response methods are supported in ogengine.

* Instant trigger: Call the registered listener immediately after receiving the screen event (default)

Advantages: Like Android, it is easy to learn
Disadvantage: Pay attention to the processing time. If the timeout time is reached, no response is returned.
It is easy to compete with the updatethread thread or plotting thread in ogengine to form resources
To avoid competition, thread security is complicated.

* Trigger in updatethread: After receiving the screen event, it is first put into the pool. The Unified Call in updatethread is opposite to the above one.


Preprocessing Flow

Engine. ontouch (): determines whether the engine is running. If it is stopped, it will not do anything. Otherwise, it will be handed over to mtouchcontroller for processing.

Singletouchcontroler. onhandlemotionevent (): has two branches

* Instant trigger:

Touchevent. Obtain (): Generate touchevent
Mtoucheventcallback. ontouchevent (): Response event (call engine. ontoucheven

* Triggered in updatethread:
Touchevent. Obtain (): Generate touchevent

Mtoucheventrunnablepoolupdatehandler. obtainpoolitem (): Generate event processing object
The event processing object is to respond to the event through mtoucheventcallback. ontouchevent () in the run () method.
Toucheventrunnablepoolitem. Set (touchevent): sets screen events.
Mtoucheventrunnablepoolupdatehandler. postpoolitem: put into the event pool
In engine. onupdate, call mtouchcontroller. onupdate () to execute event processing.


Screen event processing


A) Engine. ontouchevent (): entry to screen event processing

B) camera. convertsurfacetoscenetouchevent (): Coordinate Transformation

C) engine. ontouchscene (): Process scene screen events (traverse the registered toucharea and call the corresponding processing listener if appropriate, if there is no scene listener, scene has three interesting properties in screen processing:
Monareatouchtraversalbacktofront: whether the event response sequence is forward or forward. The default value is forward from the back.
Mtouchareabindingenabled: Specifies whether to bind an action_down event to the listener. Binding means that all events until action_up or action_cancel are handled by the listener.
Monscenetouchlistenerbindingenabled: similar to the last time, the bound object is the listener of scene.

Http://www.eoeandroid.com/forum-863-1.html

Www.ogengine.com



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.