[Flash Development Notes] How to use custom events in as2.0

Source: Internet
Author: User
In as2 programming, we usually need to process some asynchronously loaded data, which is a bit similar to callback in Ajax, that is, we do not know when the data will be returned, and only when the data is returned, execute the operation we defined.
In flash6 and the past, we often encountered loading an image or a piece of text from the outside. However, when the data is loaded successfully, it needs to be done in a special way, for example, if you add a special mark at the end of the text and then use loop detection, when the mark is read, it is deemed that the data is loaded successfully.

Of course, these problems do not exist in as2.0. They are generally used in both image loading and external text loading.Onloadcomplete,Onloadprogress,OncompleteTo listen for the loading process or complete the action.

What we want to talk about today is how we can customize our own events in a custom class. For example, we have our own class, which encapsulates the operations that use remoting to read data from the database. when the data is read, The remoting resultevent or faultevent will be executed. At this time, how can we return this result to the script segment of this class through the event of the class, so that the code looks more beautiful, more Oo, and more accurate.

The following uses an example to describe how to use a custom event. In this example, flash 8 + remoting +. NET Framework + IIS is required. Make sure that you have these prerequisites.

1. Create a virtual directory yaogame in IIS and set. Net to 1.1 or 2.0;

2. Install flash remoting for. Net to this virtual directory, or the directory contains files:
Bin \ flashgateway. dll
Bin \ frconfig.txt
Gateway. aspx
Web. config
Getdata. aspx

(You can download these files from the back of this article and copy them directly. Friends who have worked in remoting development will naturally know where they are used .)

Note:Web. configMedium

<Httpmodules>
<Add name = "gatewaycontroller" type = "flashgateway. Controller. gatewaycontroller, flashgateway"/>
</Httpmodules>

The getdata. aspx content is as follows:

<% @ Page Language = "C #" DEBUG = "true" %>

<% @ Register tagprefix = "Macromedia" namespace = "flashgateway" assembly = "flashgateway" %>
<% @ Import namespace = "system. Data" %>

<Macromedia: Flash id = "flash1" runat = "server"/>

<Script language = "C #" runat = "server">
Void page_load (Object sender, eventargs E)
{
// ================================================ ==============================
// Author: Joseph. Yao (http://yao.cnblogs.com)
// Create Date: 2007-7-18 22:21:27
// ================================================ ==============================

If (flash1.params. Count> 0)
{
If (flash1.params [0] = NULL)
{
Flash1.result = "Hello world .";
}
Else
{
Flash1.result = "hello" + flash1.params [0]. tostring () + ".";
}
}
}
</SCRIPT>

3. Create a testclass class with the following content:

Import MX. remoting. Service;
Import MX. remoting. pendingcall;
Import MX. remoting. recordset;
Import MX. rpc. relayresponder;
Import MX. rpc. resultevent;
Import MX. rpc. faultevent;
Import MX. remoting. Debug. netdebug;
// Netdebug. initialize ();
Import MX. Services. log;
/**
*
* @ Author: Joseph. Yao
* @ Version: XX. x. x
* @ Create Date: 2007-07-18
* @ Description: User-Defined event demo in as2.0.
*
**/
Class testclass {
VaR gatewaypath: string;
VaR nickname;
// Constructor
Function testclass (){
// Initialize the gateway address
Gatewaypath = "http: // localhost/yaogame/gateway. aspx ";
}
Function remotedata (resultfun: String, faultfun: string ){
//
// Read data through remoting, but it cannot be estimated how long it will take to return
//
// Note that the yaogame here should be consistent with the virtual directory
// The three rows are remoting to load data from the. NET program. The call is http: // localhost/yaogame/getdata. aspx.
VaR myresponder = new relayresponder (this, resultfun, faultfun );
// Record the log: var service: service = new service (gatewaypath, new log (), "yaogame", null, myresponder );
VaR service: service = new service (gatewaypath, null, "yaogame", null, myresponder );
VaR PC: pendingcall = service. getdata (this. Nickname );
}
// This method is executed when remoting returns data successfully.
Function remote_result (EVT: resultevent): void {
//
// {The loaded data (EVT. Result) can be processed here}
//
// When loading is successful, an event message is sent. The value of success is true.
Complete(True, string (EVT. Result ));
}
// This method is executed when an error occurs when remoting returns data.
Function remote_fault (EVT: faultevent): void {
Trace ("failed to load data:" + EVT. fault. faultstring );
// When loading fails, an event message is also sent, but the success value is false,
Complete(False, "load data error .");
}
// A time-consuming operation cannot predict how long the operation can be completed, such as reading data from the database.
Function dosomething (Nickname: string ){
This. Nickname = nickname;
Remotedata ("remote_result", "remote_fault ");
}
// Broadcast successful messages and data through custom events
Function Complete(Success: Boolean, MSG: string ){
OBJ. oncomplete(Success, MSG );
}
//
// ==================================== Event ====================== ==============
//
Private var OBJ;
// An event that occurs when a time-consuming operation is completed is actually an attribute of the as2.0 class, but the attribute is passed as a function object;
Public Function set Oncomplete( Fun: Object): void {
// Note the new here. If you "Link" the current class to the MC in the library, pay attention to it here, if you do not use the new feature, you may not want it.
// The best way is to instantiate an OBJ every time the event is "delegated;
OBJ = new object ();
OBJ. oncomplete = Fun;
}
}

4. CreateCustomeventdemo. flaTo test this class, add the code using this class in the first frame of the FLA timeline:

VaR Tc = new testclass ();
// Specify an event execution process for the oncomplete event of the class instance, that is, when the data is complete, the script in this function is executed and the data is returned;
TC. Oncomplete= Function (success: Boolean, MSG: string ){
// Success indicates whether the data is loaded successfully, and MSG indicates the returned data.
Trace (SUCCESS );
Trace (MSG );
};
TC. dosomething ("Joseph. Yao ");
// After the execution is successful, return:
// True
// Hello Joseph. Yao.

Last runCustomeventdemo. flaFile to view the test results.

5. The usage of custom events in the code above has been described in detail. Adding the blacklist and bold parts is the key code. In fact, the Custom Event in this example can only be attached to one event, that is, unicast. If you want to implement event multicasting, it is a little more complicated, you can maintain an event list and want to discuss it with you.
Pay attention to the Flash Security Policy when testing this example. There should be a crossdomain under http: // localhost/root. attackers can use XML files to authorize accessible domains. Otherwise, they will be depressed if they do not see the results.

Download the sample source code in this article (33 k):/files/Yao/as2customeventdemo.rar

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.