Before using this lesson, you should be acquainted with the timeline, frames, keyframes, drawing shapes, modifying objects, symbols, and symbol instances.
This lesson will cover two types of interaction. The simpler type of interaction is state change on mouseover or click. The second type of interaction is state change on dragging and dropping (or dragging and releasing).
Creating an interaction requires at least one scriptable object (movie clip or button) and ActionScript that targets the object. Button objects have some built-in interactions—automatic state changes for mouseover and mousedown events. You can create more powerful and complex interactions using movie clips or movie clips in combination with buttons. Try out the various interactions in the example below.
Download the sample file: interaction.zip (12 KB).
In the example ActionScript below, you will see lines marked with "//" and highlighted. These lines are comments, which explain the purpose of the preceding line of script.

stop(); action in the first keyframe.stop(); action in the "on" keyframe.

toggle_mc.onRollOver = function() {
// "toggle_mc" is the name of movie clip instance. "onRollOver" is the event handler
toggle_mc.gotoAndStop("on");
// tells toggle_mc to change to the "on" frame
}
toggle_mc.onRollOut = function() {
toggle_mc.gotoAndStop("off");
// tells toggle_mc to change to the "off" frame
}

tog2state = "off";
toggle2_mc.onRelease = function() {
if (tog2state == "off") {
// if the current state is "off"
toggle2_mc.gotoAndStop("on");
// show the "on" frame
tog2state = "on";
// set the variable to reflect the current state
} else {
// otherwise,
toggle2_mc.gotoAndStop("off");
// show the "off" frame
tog2state = "off";
// set the variable to reflect the current state
}
}


var perc_str = "0%"; // create a variable that will hold the text to be displayed slider_mc.onPress = function() { // when you press and hold on the slider movie clip startDrag(this, false, 0, 0, 400, 0); // start dragging the slider movie clip ("this") // do not lock it to the center of the mouse position ("false") // constrain the slider's movements to the coordinates left 0, top 0, right 400, bottom 0 } slider_mc.onRelease = function() { // when you release the slider movie clip stopDrag(); // stop dragging the slider movie clip } var mouseListener = new Object(); // create a listener object (listener objects "listen" to events such as a mouse or key press) mouseListener.onMouseDown = function() { // when the mouse button is pressed this.isDragging = true; // set a variable to let the mouse listener know that it is dragging } mouseListener.onMouseUp = function() { this.isDragging = false; } mouseListener.onMouseMove = function() { // when the mouse is moved if (this.isDragging) { // if the mouse is dragging var perc_num = slider_mc._x / 4; // set the percentage number to the x-coordinate of the slider, divided by 4 // (since the slider can move up to 400 pixels) perc_str = perc_num + "%"; // put the percent symbol after the percentage number counter_txt.text = perc_str; // display the percentage text in the dynamic text box } } Mouse.addListener(mouseListener); // add the listener object to the Mouse class counter_txt.text = perc_str; //display the initial percentage string in the dynamic text box
dropper_mc.onPress = function() {
// when you press and hold on the dropper movie clip
startDrag(dropper_mc);
// start dragging it
}
dropper_mc.onRelease = function() {
// when you release the dropper movie clip
stopDrag();
// stop dragging it
if (eval(this._droptarget) == target_mc) {
// if it is dropped on the target,
dropper_mc.gotoAndStop("on");
// toggle the dropper to "on"
} else {
// otherwise,
dropper_mc.gotoAndStop("off");
// toggle the dropper to "off"
}
}
Lesson index | Flash overview | Animation with playback controls | Interactive graphic | Importing external media in Flash | Common pitfalls in HTML and CSS | Embedding media objects in HTML