Problem: Create an interactive button in a programmatic way, and let the user click it to perform an action.Solution: Create a simplebutton class instance and create a display object for upstate, downstate, overstate, and hitteststate. You can also create a subclass of simplebutton to describe the desired button behavior.
Body:
Use the simplebutton class to create a button. Simplebutton class allows users to use the mouse and display objects
Interaction: You can use several buttons to define the interaction mode. The status is a property of the simplebutton class.
Upstate: The Display object in the "up" status by default. If the mouse is not on the button, it is "up.
Overstate: When you move the cursor over a button, it is used to determine whether the button looks like the Display object above it.
Downstate: The displayed object when the button is pressed.
Hitteststate: Defines the Display object popped up by a button. This status is not displayed on the screen and is used to track the mouse.
Paste an instance to describe the four status attributes of the button to define the button status:
Package {import flash. display. *; import flash. events. *; public class simplebuttondemo extends sprite {public function simplebuttondemo () {// create a simple button and configure the position var button: simplebutton = new simplebutton (); button. X = 20; button. y = 20; // button in different states of the create button. upstate = createcircle (0x00ff00, 15); button. overstate = createcircle (0 xffffff, 16); button. downstate = createcircle (0 xcccccc, 15); button. hitteststate = button. upstate; // Add the listener button for the click event. addeventlistener (mouseevent. click, handleclick); // Add to the display list addchild (button);} private function createcircle (color: uint, radius: Number): Shape {var circle: shape = new shape (); circle. graphics. linestyle (1, 0x000000); circle. graphics. beginfill (color); circle. graphics. drawcircle (0, 0, radius); circle. graphics. endfill (); Return circle;} private function handleclick (Event: mouseevent): void {trace ("Mouse clicked on the button ");}}}
Note: This is usually the case when the hitteststate property is set to the same Display object as the upstate property, because when the user mouse is within the boundary of the upstate Display object, the button should be activated.