When building applications or games in Flash, often you might want a button to continually dispatch an event for as long as a user has the Button instance pressed. For example, if you were building a custom numeric stepper you would want the numbers to increase or decrease for as long as the user pressed the arrow keys. Similarly, if you were building a game, you may want the character to keep running for as long as the user clicks a button. The Button component has an autoRepeat property (inherited from the fl.controls.BaseButton class) which controls whether the buttonDown event (represented by the fl.events.ComponentEvent.BUTTON_DOWN constant) is dispatched more than one time when the user holds the mouse button down over the component.
Using the repeatDelay and repeatInterval styles (inherited from the fl.controls.LabelButton class), you can control the delay before the initial buttonDown event, and the frequency at which the event is dispatched while the button is pressed. The repeatDelay style determines the number of milliseconds (ms) to wait after the buttonDown event is first dispatched before sending a second buttonDown event (the default value is 500 ms). The repeatInterval style determines the interval, in milliseconds, between buttonDown events that are dispatched after the delay that is specified by the repeatDelay style (the default value is 35 ms). For example, if you set the repeatDelay style to 2000, and the repeatInterval style to 500, the first buttonDown event would be dispatched 2 seconds (2000 ms) after the user pressed the button, and every 500 ms for as long as the user held the button down.
The following example creates a Button instance, sets its autoRepeat property to true, and adds event listeners for the buttonDown and click events.
Full code after the jump.
// ActionScript 3.0 // Import the required component classes. import fl.controls.Button; import fl.controls.TextArea; import fl.events.ComponentEvent; var init:Boolean = false; /* Create a new Button component instance, set its autoRepeat property to true, and add it to the display list. */ var myButton:Button = new Button(); myButton.autoRepeat = true; myButton.label = "autoRepeat = true"; myButton.width = 120; myButton.move(10, 10); myButton.addEventListener(ComponentEvent.BUTTON_DOWN, buttonDownHandler); myButton.addEventListener(MouseEvent.CLICK, clickHandler); addChild(myButton); // Create a new TextArea component instance, and add it to the display list. var debugTextArea:TextArea = new TextArea(); debugTextArea.setSize(200, 100); debugTextArea.move(10, 40); addChild(debugTextArea); /* Handler function for the Button component instance. This function gets called whenever the button instance dispatches the buttonDown event. This function checks the value of the init variable, and if true clears any existing text from the text area, next a string is appended to the debugTextArea instance, and scrolls the text area to the last line. */ function buttonDownHandler(evt:ComponentEvent):void { if (init) { debugTextArea.text = ""; init = false; } debugTextArea.appendText("[" + getTimer() + "ms] " + evt.type + "\n"); debugTextArea.verticalScrollPosition = debugTextArea.maxVerticalScrollPosition; } /* Handler function for the Button component instance. This function gets called whenever the button instance dispatches the click event. This function appends a string to the debugTextArea instance, and scrolls the text area to the last line. Finally, the init function is reset to true. */ function clickHandler(evt:MouseEvent):void { debugTextArea.appendText("[" + getTimer() + "ms] " + evt.type + "\n"); debugTextArea.verticalScrollPosition = debugTextArea.maxVerticalScrollPosition; init = true; }
For more information on the Flash/ActionScript 3.0 Button component, see the “Using the Button component” Flash Quick Start on Adobe.com.
{ 6 comments… read them below or add one }
Hi,
im developing my flash site to hold a portfolio. The idea was to create a movie clip with a series of images, each on their own keyframe, then within this movie clip a next and previous button will be available to cycle through the images. Only problem i am having is working out the AS for the buttons. A few attempts have left me skipping a frame on the main timeline but i want to skip a frame within the movieclip.
Could someone help please? im using AS2 by the way.
Thanks, Iain
I am having problems with
import fl.events.ComponentEvent;
I get the following error:
1172: Definition fl.events:ComponentEvent could not be found.
So far, I have not been able to determine what this means. What did I forget?
Tom,
I’m not sure if it would matter, but do you have an ActionScript 3.0 component in your Flash document’s library?
Peter
Hi Peter,
I think so, how can I be sure?
Tom
I’d open the Library Panel and make sure you have at least 1 ActionScript 3.0 component added.
Or, if you’re not sure, try dragging a Label component from the Component’s panel onto the Stage, and then delete it. (Deleting it will remove it from the Stage, but leave it in the document’s library.)
Peter
Hi Peter,
That worked! Thank you. It seems rather silly that this needed to be done, but I’m glad it’s working now :)
Tom