13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Drag and drop in AIR<br />

SecurityErrorEv<strong>en</strong>t.SECURITY_ERROR — Informs the runtime that a security error has occurred. The runtime<br />

aborts file creation and calls the IFilePromise close() method.<br />

HTTPStatusEv<strong>en</strong>t.HTTP_STATUS — Used, along with httpResponseStatus, by the runtime to make sure that<br />

the data available repres<strong>en</strong>ts the desired cont<strong>en</strong>t, rather than an error message (such as a 404 page). Objects based<br />

on the HTTP protocol should dispatch this ev<strong>en</strong>t.<br />

HTTPStatusEv<strong>en</strong>t.HTTP_RESPONSE_STATUS — Used, along with httpStatus, by the runtime to make sure<br />

that the data available repres<strong>en</strong>ts the desired cont<strong>en</strong>t. Objects based on the HTTP protocol should dispatch this<br />

ev<strong>en</strong>t.<br />

The data provider should dispatch these ev<strong>en</strong>ts in the following sequ<strong>en</strong>ce:<br />

1 op<strong>en</strong> ev<strong>en</strong>t<br />

2 progress or socketData ev<strong>en</strong>ts<br />

3 complete or close ev<strong>en</strong>t<br />

Note: The built-in objects, FileStream, Socket, and URLStream, dispatch the appropriate ev<strong>en</strong>ts automatically.<br />

The following example creates a file promise using a custom, asynchronous data provider. The data provider class<br />

ext<strong>en</strong>ds ByteArray (for the IDataInput support) and implem<strong>en</strong>ts the IEv<strong>en</strong>tDispatcher interface. At each timer ev<strong>en</strong>t,<br />

the object g<strong>en</strong>erates a chunk of data and dispatches a progress ev<strong>en</strong>t to inform the runtime that the data is available.<br />

Wh<strong>en</strong> <strong>en</strong>ough data has be<strong>en</strong> produced, the object dispatches a complete ev<strong>en</strong>t.<br />

package<br />

{<br />

import flash.ev<strong>en</strong>ts.Ev<strong>en</strong>t;<br />

import flash.ev<strong>en</strong>ts.Ev<strong>en</strong>tDispatcher;<br />

import flash.ev<strong>en</strong>ts.IEv<strong>en</strong>tDispatcher;<br />

import flash.ev<strong>en</strong>ts.ProgressEv<strong>en</strong>t;<br />

import flash.ev<strong>en</strong>ts.TimerEv<strong>en</strong>t;<br />

import flash.utils.ByteArray;<br />

import flash.utils.Timer;<br />

[Ev<strong>en</strong>t(name="op<strong>en</strong>", type="flash.ev<strong>en</strong>ts.Ev<strong>en</strong>t.OPEN")]<br />

[Ev<strong>en</strong>t(name="complete", type="flash.ev<strong>en</strong>ts.Ev<strong>en</strong>t.COMPLETE")]<br />

[Ev<strong>en</strong>t(name="progress", type="flash.ev<strong>en</strong>ts.ProgressEv<strong>en</strong>t")]<br />

[Ev<strong>en</strong>t(name="ioError", type="flash.ev<strong>en</strong>ts.IOErrorEv<strong>en</strong>t")]<br />

[Ev<strong>en</strong>t(name="securityError", type="flash.ev<strong>en</strong>ts.SecurityErrorEv<strong>en</strong>t")]<br />

public class AsyncDataProvider ext<strong>en</strong>ds ByteArray implem<strong>en</strong>ts IEv<strong>en</strong>tDispatcher<br />

{<br />

private var dispatcher:Ev<strong>en</strong>tDispatcher = new Ev<strong>en</strong>tDispatcher();<br />

public var fileSize:int = 0; //The number of characters in the file<br />

private const chunkSize:int = 1000; //Amount of data writt<strong>en</strong> per ev<strong>en</strong>t<br />

private var dispatchDataTimer:Timer = new Timer( 100 );<br />

private var op<strong>en</strong>ed:Boolean = false;<br />

public function AsyncDataProvider()<br />

{<br />

super();<br />

dispatchDataTimer.addEv<strong>en</strong>tList<strong>en</strong>er( TimerEv<strong>en</strong>t.TIMER, g<strong>en</strong>erateData );<br />

}<br />

public function begin():void{<br />

dispatchDataTimer.start();<br />

}<br />

Last updated 6/6/2012<br />

627

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!