344
Handling Events
For example, consider the following two event handlers. The first is an
onPress
event handler
associated with a movie clip named
clip_mc
. The second is an
on()
handler attached to the
same movie clip instance.
// Attached to clip_mc's parent clip timeline:
clip_mc.onPress = function () {
var shoeColor; // local function variable
shoeColor = "blue";
}
// on() handler attached to clip_mc:
on (press) {
var shoeColor; // no local variable scope
shoeColor = "blue";
}
Although both event handlers contain the same code, they have different results. In the first
case, the
color
variable is local to the function defined for
onPress
. In the second case,
because the
on()
handler doesn’t define a local variable scope, the variable is defined in the
scope of the timeline of the
clip_mc
movie clip.
For
on()
event handlers attached to buttons, rather than to movie clips, variables (as well as
function and method calls) are invoked in the scope of the timeline that contains the
button instance.
For instance, the following
on()
event handler produces different results that depend on
whether it’s attached to a button or movie clip object. In the first case, the
play()
function
call starts the playhead of the timeline that contains the button; in the second case, the
play()
function call starts the timeline of the movie clip to which the handler is attached.
// Attached to button.
on (press) {
play(); // Plays parent timeline.
}
// Attached to movie clip.
on (press) {
play(); // Plays movie clip's timeline.
}
When attached to a button object, the
play()
function applies to the timeline that contains
the button—that is, the button’s parent timeline. But when the
on(press)
handler is
attached to a movie clip object, the
play()
function call applies to the movie clip that bears
the handler. If you attach the following code to a movie clip, it plays the parent timeline:
// Attached to movie clip.
on (press) {
_parent.play(); // Plays parent timeline.
}
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...