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 />

Handling errors<br />

Working with error ev<strong>en</strong>ts<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The ErrorEv<strong>en</strong>t class and its subclasses contain error types for handling errors dispatched by Flash runtimes as they<br />

try to read or write data.<br />

The following example uses both a try..catch statem<strong>en</strong>t and error ev<strong>en</strong>t handlers to display any errors detected<br />

while trying to read a local file. You can add more sophisticated handling code to provide a user with options or<br />

otherwise handle the error automatically in the places indicated by the comm<strong>en</strong>t “your error-handling code here”:<br />

package<br />

{<br />

import flash.display.Sprite;<br />

import flash.errors.IOError;<br />

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

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

import flash.media.Sound;<br />

import flash.media.SoundChannel;<br />

import flash.net.URLRequest;<br />

import flash.text.TextField;<br />

import flash.text.TextFieldAutoSize;<br />

public class LinkEv<strong>en</strong>tExample ext<strong>en</strong>ds Sprite<br />

{<br />

private var myMP3:Sound;<br />

public function LinkEv<strong>en</strong>tExample()<br />

{<br />

myMP3 = new Sound();<br />

var list:TextField = new TextField();<br />

list.autoSize = TextFieldAutoSize.LEFT;<br />

list.multiline = true;<br />

list.htmlText = "Track 1";<br />

list.htmlText += "Track 2";<br />

addEv<strong>en</strong>tList<strong>en</strong>er(TextEv<strong>en</strong>t.LINK, linkHandler);<br />

addChild(list);<br />

}<br />

private function playMP3(mp3:String):void<br />

{<br />

try<br />

{<br />

myMP3.load(new URLRequest(mp3));<br />

myMP3.play();<br />

}<br />

catch (err:Error)<br />

Last updated 6/6/2012<br />

64

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

Saved successfully!

Ooh no, something went wrong!