"Stage3D Study notes continued" Cottage Starling (11): Touch Event System

Source: Internet
Author: User

Our Cottage starling version will be here to stop the update, mainly due to limited time, and our cottage version is also very good to complete his task " understand Starling's core rendering ", Next Starling parsing we will directly read the starling source code to understand the other internal operating methods.

The touch event system is undoubtedly the most important thing beyond the core rendering, and the touch event system in our notes is based on Starling v1.1.

Introduction to the Touch Event system:

There is no Interactiveobject class in the inheritance of Starling, so all display objects have interactive functionality by default. In other words, displayobject is defined as the interaction behavior.

Let's look at the features of the main classes:

    • touchevent: Touch event class, Starling native mouse and touch events uniformly encapsulated into TouchEvent class, which guarantees PC development and mobile runtime consistency, unlike native events, The TouchEvent event type has only one "Touchevent.touch".
    • Touch: A Touch object contains detailed information about a finger or mouse on the screen, and multiple touch objects if it is multi-touch.
    • touchphase: Because TouchEvent has only one event type "touch", we need to use the phase property of the touch object to distinguish between specific situations (press, move, bounce, etc.) The touchphase defines the value that exists for the phase attribute.
    • touchprocessor: The core processing class of the touch event system is the class that we are going to focus on next.
    • Touchmarker: For internal use, the PC simulates multi-touch display objects.

Touch Event Processing Flow:

Starling holds an instance of the Touchprocessor class "Mtouchprocessor", which is used to handle all touch events uniformly.

It is also important to note that the touch event will have a "delay", since Starling captures the event from the native flash layer and is not sent immediately to the object listening to the Starling event, but instead sends the internal "queue" inside, then focuses on the frame rate. However, in general, we do not set the frame frequency is too small, this delay on the user basically no impact.

Native Event Registration:

The Starling class starts with event registration for the stage:

1  Public functionStarling (XXX)2 {3     ...4     //Register Touch/mouse event handlers5      foreach (varToucheventtype:stringinchtoucheventtypes)6Stage.addeventlistener (Toucheventtype, OnTouch,false, 0,true);7     ...8 }9 Ten Private function Gettoucheventtypes (): Array One { A     returnMouse.supportscursor | | !multitouchenabled? - [Mouseevent.mouse_down, Mouseevent.mouse_move, mouseevent.mouse_up]: - [Touchevent.touch_begin, Touchevent.touch_move, Touchevent.touch_end];  the}

We can see that the registered event type is selected based on whether the mouse is supported and whether multi-touch is turned on, and all event handling is given to the Ontouch method.

Ontouch Method Processing:

1 Private functionOnTouch (event:event):void2 {3     if(!mstarted)return;4     5     varGLOBALX: Number;6     varGlobaly: Number;7     varTouchid:int;8     varphase:string;9     Ten     //Figure out general touch Properties One     if(event is MouseEvent) A     { -         varMouseevent:mouseevent =event as MouseEvent; -GlobalX =Mouseevent.stagex; theGlobaly =Mouseevent.stagey; -TouchID = 0; -          -         //Mouseevent.buttondown returns True for both left and right button (AIR supports +         //The right mouse button). We only want to react on the left button for now, -         //So we had to save the state for the left button manually. +         if(Event.type = = mouseevent.mouse_down) Mleftmousedown =true; A         Else if(Event.type = = mouseevent.mouse_up) Mleftmousedown =false; at     } -     Else -     { -         varTouchevent:touchevent =event as TouchEvent; -GlobalX =Touchevent.stagex; -Globaly =Touchevent.stagey; inTouchID =Touchevent.touchpointid; -     } to      +     //Figure out Touch phase -     Switch(Event.type) the     { *          CaseTouchEvent.TOUCH_BEGIN:phase = Touchphase.began; Break; $          CaseTouchEvent.TOUCH_MOVE:phase = touchphase.moved; Break;Panax Notoginseng          CaseTouchEvent.TOUCH_END:phase = touchphase.ended; Break; -          CaseMouseEvent.MOUSE_DOWN:phase = Touchphase.began; Break; the          CaseMouseEvent.MOUSE_UP:phase = touchphase.ended; Break; +          CaseMouseevent.mouse_move: APhase = (Mleftmousedown? TouchPhase.MOVED:TouchPhase.HOVER); Break; the     } +      -     //move position into viewport bounds $GlobalX = Mstage.stagewidth * (globalx-mviewport.x)/Mviewport.width; $Globaly = Mstage.stageheight * (GLOBALY-MVIEWPORT.Y)/Mviewport.height; -      -     //Enqueue touch in touch processor the Mtouchprocessor.enqueue (TouchID, Phase, GLOBALX, globaly); -}
View Code

The Ontouch method distinguishes between native mouse events and touch events, while acquiring some basic information, such as global coordinates.

Eventually, the collected core information is called to the Mtouchprocessor.enqueue method to join the pending queue.

Touch event handling and throwing:

The Mtouchprocessor.enqueue method collects the events that need to be handled per frame, noting that this time just gets some mouse or touch information from the native stage.

The touch event that eventually generates Starling is in the AdvanceTime method that is called in the Onenterframe method of Starling:

1 Private functionAdvanceTime ():void2 {3     varNow Number= Gettimer ()/1000.0;4     varPassedtime: Number= Now-Mlastframetimestamp;5Mlastframetimestamp =Now ;6     7 Mstage.advancetime (passedtime);8 Mjuggler.advancetime (passedtime);9 Mtouchprocessor.advancetime (passedtime);Ten}

We found that after processing the stage and animation of the AdvanceTime method, followed by the processing of Mtouchprocessor AdvanceTime method. Note that all touch event throws for Starling are performed in this method. When this is done, the rendering is performed, so if we change the state of the object in the touch event, we can see the effect in this frame.

Mtouchprocessor's AdvanceTime method handles all the touch event messages collected, cleans up the old tap tag (because the tap operation takes a longer life cycle, so extra processing), updates the existing Touch object , the Mqueue queued event arguments are processed to produce a new touch object ( the target target is found under the global coordinates through the HitTest method ) and, if necessary, Let this touch event correspond to the object to distribute the event.

Use of touch events:

1. Listen for an object that requires touch monitoring:

1 sprite.addeventlistener (Touchevent.touch, OnTouch);

2. Processing in the Ontouch method:

1 Private functionOnTouch (e:touchevent):void2 {3     //gets multiple Touch objects, one for one touch, and multi-touch if there are multiple objects .4     varTouches:vector.<touch> =e.touches;5     //only one Touch .6     if(Touches.length = = 1)7     {8         //...9     }Ten     //two touches One     if(Touches.length = = 2) A     { -         //... -     } the}

3. Obtain the specific touch object that contains the required details;

I just have a simple way to use it, and more detailed ways to use it can be viewed in the following ebook:

Http://www.starlinglib.com/wiki/OutsideDocs.html

"Stage3D Study notes continued" Cottage Starling (11): Touch Event System

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.