Working with Buttons


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.

  • onPress An onPress event handler is invoked when the mouse is pressed while its pointer is over the hit area of the movie clip to which the event is attached.

  • onRelease The onRelease event handler is invoked when the mouse is released, while its pointer is over the hit area of the movie clip.

  • onReleaseOutside Like onRelease, onReleaseOutside is invoked when the mouse is released. The difference is that onReleaseOutside is invoked when the mouse pointer is outside the hit area of the movie clip.

  • onRollOut The onRollOut handler is invoked when the mouse pointer rolls out of the hit area of the movie clip. This is often paired with the onRollOver event to trigger events based on the user moving the mouse over a movie clip and then out again.

  • onRollOver The onRollOver event is triggered when the mouse pointer enters the hit area of the movie clip. A common effect is to change the color or alpha value of the clip to indicate to users that they have encountered an element of the user interface.



Special Edition Using Macromedia Studio 8
Special Edition Using Macromedia Studio 8
ISBN: 0789733854
EAN: 2147483647
Year: 2003
Pages: 337

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net