Chapter 10, "Animation Basics," discussed the three types of symbols in Flash: graphic, button, and movie clip. Buttons and movie clips are special. They are objects, and you can create instances of them. They can accept user input from external devices, such as the mouse, send this event to ActionScript, and accept instructions from ActionScript. This all requires that the instance have a unique instance name. If the instance is on the stage, you can give it an instance name in the Property inspector. A button is a specialized movie clip, with four unique frames. Three frames respond to different mouse states (up, over, and down) and one defines the area that responds to the mouse (hit). Button objects have built-in methods and properties, and you can use these to control a button instance if you attach a function to an instance's button event. In this example, a function is first defined, and then this function is attached to the button's onRelease method. Now, when the user releases the mouse after clicking on the button, the button is redrawn 10 pixels to the right of its current position. // move a movie clip 10 pixels to the right function moveClip(){ this._x +=10; } // attach moveButton function to the circle circle_btn.onRelease = moveClip; Another way to attach a function to a button event is to use a shorthand notation that combines the function definition and the function attachment. In this example, the function is defined inside the onRelease method: circle_btn.onRelease(){ this._x +=10; } This can be a quick way to attach code to a button event, but it's not as reusable as attaching a previously defined function to the event. It's fine for when you want to attach unique code that will not be applied to other instances. There are many button event methods available in Flash. Button event methods are also called event handlers. I've listed a few of them here.
|