EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 345

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Event handler scope 345
Within an event handler or event listener definition, the same play() function applies to the
timeline that contains the function definition. For example, suppose you declare the following
my_mc.onPress event handler method on the timeline that contains the my_mc movie
clip instance:
// Function defined on a timeline
my_mc.onPress = function () {
play(); // plays timeline that it is defined on.
};
To play the movie clip that defines the onPress event handler, refer explicitly to that clip
using the
this keyword, as follows:
// Function defined on root timeline
my_mc.onPress = function () {
this.play(); // plays timeline of my_mc clip.
};
However, the same code placed on the root timeline for a button instance would instead play
the root timeline:
my_btn.onPress = function () {
this.play(); // plays root timeline
};
For more information about the scope of the this keyword in event handlers, see “Scope of
the this keyword” on page 347.
ActionScript 2.0 example The following TextLoader class is used to load a text file and
display some text after it successfully loads the file.
// TextLoader.as
class TextLoader {
private var params_lv:LoadVars;
public function TextLoader() {
params_lv = new LoadVars();
params_lv.onLoad = onLoadVarsDone;
params_lv.load("http://www.helpexamples.com/flash/params.txt");
}
private function onLoadVarsDone(success:Boolean):Void {
_level0.createTextField("my_txt", 999, 0, 0, 100, 20);
_level0.my_txt.autoSize = "left";
_level0.my_txt.text = params_lv.monthNames; // undefined
}
}

Table of Contents

Related product manuals