Java and flex Study Notes (3) -- Understanding the event mechanism in Flash

Source: Internet
Author: User
Tags event listener

In the last test, when we click "send", flex interacts with the Java method defined in the background, returns the result to flex, and displays it on the foreground page, in fact, this depends on events. Events run through all the processes in flex. If there are no events, there will be no human-computer interaction.


Flash supports event programming in the ActionScript language. In ActionScript, each event is represented by an event object. The event object is an instance of the Flash. Events. event class or a subclass of it. The event object not only stores information about specific events, but also contains methods to facilitate the operation of event objects. The event object has the following two purposes:


● The event object stores the information of a specific event in a group of attributes to represent the actual event.


● An event object contains a set of methods that can be used to operate the event object and affect the behavior of the event processing system.


After an event object is created, You can dispatch the event object by using the API method of ActionScript. The so-called "dispatch" means to pass the event object to other objects in order according to certain rules and execute the event listener registered on these objects.


These objects that can listen to the event are called the target object of the event. We can write an event listener to process the events passed to the target object.


The event listener is a self-compiled function and method used to respond to specific events. The event listener can be a function and method of the target object, or a function and method of other objects. However, the event listener must register on the target object to process the events passed to the target object.

In order to better understand the event mechanism in flash, we can use a small example to see how the event works!

 

1. Create a custom event class


In ActionScript, the Custom Event class must inherit the Flash. Events. event class. The following is a custom event class testevent. The Code is as follows:


package com.flex.ases{    import flash.events.Event;       public class TestEvent extends Event    {       public static const TEST_EVENT:String="TEST_EVENT";       public var data:Object;       public function TestEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)       {           super(type, bubbles,cancelable);       }    }}


This class customizes an event type "test_event", while data is used to store event information. The constructor of the testevent class must provide the three parameters shown above to provide the required functions for the constructor of the parent class:


Type: A string of the event type. It not only indicates the meaning of the specific event object, but also listens to the event according to the event type. The string of the event type is generally defined by a static variable, that is, it is modified by the const keyword.


Bubbles: Indicates whether the event is "Bubbling ". This will be learned later.


Cancelable: Indicates whether the default event processing on the target object can be canceled during event scheduling. This will be analyzed later.

 

2. Create an event Adapter


In ActionScript, only instances of the Flash. Events. eventdispatcher class and its subclass can distribute events. The event dispatching method calls the dispatchevent () method of the eventdispatch class or its subclass instance by using the event object to be distributed as the parameter.


In ActionScript, only Flash is available. events. the eventdispatcher class and its subclass instance can be the event target object, because only Flash. events. the eventdispatcher class and its subclass instance can register the event listener and decide how to handle the event on the target object.


The following code creates a testeventdispatch class and inherits the eventdispatcher class. The Code is as follows:


Package COM. flex. ases {import flash. events. event; import flash. events. eventdispatcher; import flash. events. ieventdispatcher; import MX. controls. alert; public class testeventdispatch extends eventdispatcher {public functiontesteventdispatch (target: ieventdispatcher = NULL) {super (target); this. addeventlistener (testevent. test_event, testeventmethod);} protected function testeventmethod (Event: testevent ): Void {// todoauto-generated method stub alert. show (event. data as string, "prompt");} public function createtestevent (): void {var testevent: testevent = new testevent (testevent. test_event); testevent. data = "you have triggered a custom event! "; This. dispatchevent (testevent );}}}


Through the above code, we can see that the createtestevent method in the testeventdispatch class creates a testevent event instance and specifies the event type as test_event, an information entry is added to the event instance. Then, we use this instance as a parameter to dispatch events.


We add an event listener to the constructor and use testeventmethod as the processing method for the test_event event type.


3. Test


The code for calling this event is as follows:


<? Xmlversion = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "minwidth =" 955 "minheight =" 600 "> <FX: SCRIPT> <! [CDATA [import COM. flex. ases. testevent; import COM. flex. ases. testeventdispatch; protected function test_clickhandler (): void {// todoauto-generated method stub var testeventdispatch: testeventdispatch = newtesteventdispatch (); testeventdispatch. createtestevent () ;}]> </FX: SCRIPT> <FX: declarations> <! -- Place non-visualelements (e.g ., services, value objects) Here --> </FX: declarations> <s: label x = "34" Y = "97" fontsize = "19" text = "click the button to trigger a Custom Event:"/> <s: button id = "test" x = "284" Y = "87" Height = "28" label = "Trigger" Click = "test_clickhandler () "fontsize =" 18 "/> </S: Application>

 

Run this page as follows:


Click the "Trigger" button to call the custom testevent event, as shown in the following figure:




Well, after such a learning, we now have a texture understanding of the flash event mechanism.


Original article, reprint please indicate the source: http://www.dianfusoft.com/showDetail.action? Article ID = 130405014424, more original content, visit: http://www.dianfusoft.com/





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.