03.05.2013 Views

FLASH® LITE™ 2.x - Adobe Help and Support

FLASH® LITE™ 2.x - Adobe Help and Support

FLASH® LITE™ 2.x - Adobe Help and Support

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

function intervalFunc(target_mc) {<br />

// display a trivial message which displays the instance name <strong>and</strong> arbitrary<br />

text.<br />

trace(target_mc+" has been loaded for "+getTimer()/1000+" seconds.");<br />

/* when the target movie clip is clicked (<strong>and</strong> released) you clear the<br />

interval<br />

<strong>and</strong> remove the movie clip. If you don't clear the interval before deleting<br />

the movie clip, the function still calls itself every second even though<br />

the<br />

movie clip instance is no longer present. */<br />

target_mc.onRelease = function() {<br />

trace("clear interval");<br />

clearInterval(this.myInterval);<br />

// delete the target movie clip<br />

removeMovieClip(this);<br />

};<br />

}<br />

var jpeg_mcl:MovieClipLoader = new MovieClipLoader();<br />

jpeg_mcl.addListener(listenerObject);<br />

jpeg_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg",<br />

this.createEmptyMovieClip("jpeg_mc", this.getNextHighestDepth()));<br />

If you work with setInterval() within classes, you need to be sure to use the this keyword<br />

when you call the function. The setInterval()function does not have access to class<br />

members if you do not use the keyword. This is illustrated in the following example. For a<br />

FLA file with a button called deleteUser_btn, add the following ActionScript to Frame 1:<br />

var me:User = new User("Gary");<br />

this.deleteUser_btn.onRelease = function() {<br />

trace("Goodbye, "+me.username);<br />

clearInterval(me.intervalID);<br />

delete me;<br />

};<br />

Then create a file called User.as in the same directory as your FLA file. Enter the following<br />

ActionScript:<br />

class User {<br />

var intervalID:Number;<br />

var username:String;<br />

function User(param_username:String) {<br />

trace("Welcome, "+param_username);<br />

this.username = param_username;<br />

this.intervalID = setInterval(this, "traceUsername", 1000, this.username);<br />

}<br />

function traceUsername(str:String) {<br />

trace(this.username+" is "+getTimer()/1000+" seconds old, happy<br />

birthday.");<br />

}<br />

}<br />

76 ActionScript language elements

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

Saved successfully!

Ooh no, something went wrong!