AS3 question - Best way to lockout buttons - actionscript-3

Hello and thanks for reading this.
I made buttons using as3 within flash but what I'd like to do is make them inactive for a few seconds when one is pressed. Normally I'd use google to solve this kind of a problem but I dont even know how to word it properly.
Thanks

You could :
Set the .enabled property to false in order to have your click event handlers disabled.
Add a new locking variable and surround all the code in your click handler with 'if(lockingVariable)'. Then all you would need to do is set this to false. Ideally, though, you'd just disable the button.
As for doing it for a few seconds, look into the timer class. This link should be helpful. The typical pattern goes something like this :
var myTimer:Timer = new Timer(1000, 1); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();
function runOnce(event:TimerEvent):void {
trace("runOnce() called # " + getTimer() + " ms");
}
All you would have to do is have a re-enabling callback as the method for line 2 and your button would be disabled for 1 second.

Try using this as a base class for your buttons:
package
{
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.events.Event;
public class MyButton extends SimpleButton
{
// vars
public const DELAY:uint = 30;
private var _timer:int = 0;
/**
* Constructor
*/
public function MyButton()
{
addEventListener(MouseEvent.CLICK, _click);
}
/**
* Called on Event.ENTER_FRAME
*/
private function _handle(e:Event):void
{
_timer --;
if(_timer < 1) removeEventListener(Event.ENTER_FRAME, _handle);
}
/**
* Called on MouseEvent.CLICK
*/
private function _click(e:MouseEvent):void
{
if(_timer > 0) return;
_timer = DELAY;
addEventListener(Event.ENTER_FRAME, _handle);
// do your stuff below
clickAction();
}
/**
* Override this and fill with your actions
*/
protected function clickAction():void
{
trace("override me");
}
}
}
Here's an example of overriding the clickAction() method in MyButton:
package
{
public class MyPlayButton extends MyButton
{
override protected function clickAction():void
{
trace("play button clicked");
}
}
}

The way I would do it is simply set the enabled property of the button to false for a set amount of time, using a Timer, once the button is pressed.
myBut.addEventListener(MouseEvent.CLICK, doStuff);
function doStuff(e:MouseEvent){
//write whatever the button does here
disableBut();
}
function disableBut(){
myBut.enabled = false;
var timer:Timer = new Timer(3000, 1);
timer.addEventListener(TimerEvent.TIMER, enableBut);
timer.start()
}
function enableBut(e:TimerEvent){
myBut.enabled = true;
}
Remember that the length of time that the button is disabled for is set in the first parameter of the Timer() constructor, and is in milliseconds. In my example you can see that myBut is disabled for 3 seconds.

Related

Modify a variable when an event is triggered in AS3

I am trying to code a simple notification class in as3, it as just two buttons, one for "ok", one for "cancel", so there is one event listener for each buttons.
The problem is that I want a variable to be passed from the main class to the notification class, then to be returned to the main after one of the eventListener has been triggered by the user.
I have tried differents things but no one seems to work, I am sure there is a common and simple way to do that but I am new to the language.
Thanks, and sorry for my english.
Here is an example:
Main.as:
package {
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite {
[SWF(width="640",height="480",backgroundColor="#ffffff")]
public function Main():void {
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
var msg:Notification = new Notification();
addChild(msg);
}
}
}
Notification.as:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormatAlign;
import flash.text.TextFieldAutoSize;
public class Notification extends Sprite {
private var background:Sprite;
private var button1:Sprite;
private var button2:Sprite;
private var buttonOk:TextField;
private var buttonCancel:TextField;
public function Notification() {
addBackground();
addMessage();
addOkButton();
addCancelButton();
}
private function addBackground():void {
background = new Sprite();
background.graphics.beginFill(0x4682b4, 1);
background.graphics.drawRoundRect(0, 0, 300, 200, 30, 30);
background.graphics.endFill();
addChild(background);
}
private function addMessage():void {
message = new TextField();
message.autoSize = TextFieldAutoSize.LEFT;
message.text = "My text";
message.scaleX = 1.2;
message.scaleY = 1.2;
message.textColor = 0xffffff;
background.addChild(message);
}
private function addOkButton():void {
button2 = new Sprite();
button2.graphics.beginFill(0x35733b, 1);
button2.graphics.drawRoundRect(0, 0, 90, 40, 10, 10);
button2.x = 175;
button2.y = 115;
button2.buttonMode = true;
button2.mouseChildren = false;
button2.alpha = 0.7;
background.addChild(button2);
button2.addEventListener(MouseEvent.ROLL_OVER, onButton2);
button2.addEventListener(MouseEvent.ROLL_OUT, outButton2);
button2.addEventListener(MouseEvent.CLICK, clickButton2);
buttonOk = new TextField();
buttonOk.autoSize = TextFieldAutoSize.LEFT;
buttonOk.text = "Ok";
buttonOk.scaleX = 1.2;
buttonOk.scaleY = 1.2;
buttonOk.textColor = 0x000000;
buttonOk.selectable = false;
buttonOk.x = button2.width/2 - buttonOk.width/2;
buttonOk.y = button2.height/2 - buttonOk.height/2;
button2.addChild(buttonOk);
}
private function addCancelButton():void {
button1 = new Sprite();
button1.graphics.beginFill(0x771d1d, 1);
button1.graphics.drawRoundRect(0, 0, 90, 40, 10, 10);
button1.x = 25;
button1.y = 115;
button1.buttonMode = true;
button1.useHandCursor = true;
button1.mouseChildren = false;
button1.alpha = 0.7;
background.addChild(button1);
button1.addEventListener(MouseEvent.ROLL_OVER, onButton1);
button1.addEventListener(MouseEvent.ROLL_OUT, outButton1);
button1.addEventListener(MouseEvent.CLICK, clickButton1);
buttonCancel = new TextField();
buttonCancel.autoSize = TextFieldAutoSize.LEFT;
buttonCancel.text = "Cancel";
buttonCancel.scaleX = 1.2;
buttonCancel.scaleY = 1.2;
buttonCancel.textColor = 0x000000;
buttonCancel.x = button1.width/2 - buttonCancel.width/2;
buttonCancel.y = button1.height/2 - buttonCancel.height/2;
button1.addChild(buttonCancel);
}
private function onButton1(e:MouseEvent):void {
button1.alpha = 1;
}
private function outButton1(e:MouseEvent):void {
button1.alpha = 0.7;
}
private function onButton2(e:MouseEvent):void {
button2.alpha = 1;
}
private function outButton2(e:MouseEvent):void {
button2.alpha = 0.7;
}
private function clickButton1(e:MouseEvent):void {
this.parent.removeChild(this);
}
private function clickButton2(e:MouseEvent):void {
this.parent.removeChild(this);
}
}
}
There's no point in sending any variable to a notification and return it back.
The notification should only tell you what of its buttons was pressed.
Think about it: what if you need another notification for a different value, that's also just having an "ok" and a "cancel" button. Will you create another notification class for that?
The notification should not have any knowledge about the value or the Main class. This is to make it as independent as possible. Save your variable the roundtrip through the notification and back to main.
Instead, do something like this in Main:
create notification
listen for event of notification
event of notification will contain what happened (ok or cancel)
based on what happened, perform some logic in Main
There are other auxiliary actions that you likely want to perform, such as:
making the notification visible/invisible
making sure its modal (nothing else can be clicked when it's active)
Such actions are out of scope for this question as this is only about the flow of information.
For your Notification class, the best practice is to dispatch a custom Event. this Event carries the action performed by the user and could look something like this (untested code):
package
{
public class NotificationEvent extends Event
{
private var _action:String;
public function get action ()
{
return _action;
}
public NotificationEvent (action:String = "ok")
{
_action = action;
}
}
}
In your Notification class, dispatch that event and pass a String to the constructor that describes the action. (You could make this a lot more elaborate only allowing certain values, which would be very convenient, but it doesn't add to the overall functionality)
dispatchEvent(new NotificationEvent("cancel")); // when cancel button is clicked
in Main, a listener added to the notification will receive the Event as parameter and can extract the performed action via the action property I defined in the class, for example in a switch case:
function notificationHandler(e:NotificationEvent)
{
switch(e.action)
{
case "ok":
//do stuff
break;
case "cancel":
//do other stuff
break;
}
}
At first the custom Event seems like an overcomplication, but it provides a clear interface for the notification, without exposing any internal stuff. If you want to add keyboard control like "enter" for "ok" and "esc" for "cancel", you can do so in the Notification class, without changing anything outside. Just make sure to dispatch the NotificationEvent.
You often find code that tries stunts like notification.okBtn.addEventListener... from the Main class which works at first, but blows up quickly if you apply changes.

as3 - dispatch mouse event from external class

I am having problems understanding correctly how to dispatch events and capture them in another class.
In this case, I am trying to emulate a mouse click dispatched from an "clickM" class.
On stage I have 2 movieclips to test, a custom cursor and the listeners to capture the click event.
clickM:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event; //dispatcher
import flash.events.MouseEvent;// mouse event
public class clickM extends MovieClip {
private var delay: uint = 3000;
private var repeat: uint = 0; //se va por todo el tiempo
private var myTimer: Timer = new Timer(delay, repeat);
public function clickM() {
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}
private function timerHandler(e: TimerEvent): void {
//repeat--;
//statusTextField.text = ((delay * repeat) / 1000) + " seconds left.";
trace ( "simulate click...");
//dispatchEvent(new MouseEvent(MouseEvent.CLICK));
this.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
}
}
}
Stage code, rojo & morado are movieclips:
import flash.events.MouseEvent;
stage.addEventListener(Event.ENTER_FRAME, myFunction);
var mano: clickM = new clickM();
mano.name = "mano";
addChild (mano);
morado.addEventListener(MouseEvent.CLICK, cl);
rojo.addEventListener(MouseEvent.CLICK, cl);
stage.addEventListener(MouseEvent.CLICK, cl);
function myFunction(event: Event) {
mano.x = mouseX;
mano.y = mouseY;
}
function cl(e: MouseEvent) {
trace("click over " + e.target.name);
}
If I click over morado or rojo, there's no problem - I can get their names. If I just let the code run, I can't get their names, I just get "mano", which is the custom cursor I'm using.
How can I get the desired behavior?
Regards.
Add mouseEnabled=false; inside clickM constructor. This should make Flash to ignore your mano in the event dispatch phase, so the underlying object should be the primary target if there's any, otherwise the target will be the stage. If your custom cursor contains more movie clips, you should also add mouseChildren=false;.
public function clickM() {
mouseEnabled=false;
// possibly add this too
mouseChildren=false;
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}

making a symbol move by keyboard not showing result and when published it not reads stop(); but replays it again and again

I am new to actionscript ,
My document class is ,
package
{
//list of our imports these are classes we need in order to
//run our application.
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class engine extends MovieClip
{
// moved ourShip to a class variable.
private var Circle:circle = new circle()
//our constructor function. This runs when an object of
//the class is created
public function engine()
{
addFrameScript(0, frame1);
addFrameScript(1, frame2);
}
// frame 1 layer 1 --------------------------------------------------
public function frame1()
{
stop();
}
//-------------------------------------------------------------------
// frame 2 layer 1 --------------------------------------------------
public function frame2()
{
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
//-------------------------------------------------------------------
}
}
i made two frames first contains button and the other circle which i want to move but it not moves and it stays in the middle on second frame
My button class is
package
{
//imports
import flash.events.MouseEvent;
import flash.display.SimpleButton;
import flash.display.MovieClip;
//-------
public class start extends SimpleButton
{
public function start()
{
addEventListener(MouseEvent.CLICK, onTopClick);
addEventListener(MouseEvent.MOUSE_OVER, onBottomOver);
}
function onTopClick(e:MouseEvent):void
{
MovieClip(root).gotoAndStop(2)
}
function onBottomOver(e:MouseEvent):void
{
}
}
}
And my as of circle movieclip is
package
{
//imports
import flash.display.MovieClip;
import flash.display.Stage;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class circle extends MovieClip
{
private var speed:Number = 0.5;
private var vx:Number = 0;
private var vy:Number = 0;
private var friction:Number = 0.93;
private var maxspeed:Number = 8;
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
x+=vx;
y+=vy
}
function keyHit(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.RIGHT :
vx+=speed;
break;
case Keyboard.LEFT :
vx-=speed;
break;
case Keyboard.UP :
vy-=speed;
break;
case Keyboard.DOWN :
vy+=speed;
break;
}
}
}
}
I am sorry to post so much for you guys to read but stackoverflow is the only website where anyone helps me !
You have made several major errors. First, addFrameScript() isn't a proper way to place code on frames, use Flash's editor to place code on timeline. (IIRC you will have to make a single call out of your two in order to have all the code you add to function) And, whatever code you added to a frame of a MC is executed each frame if the MC's currentFrame is the frame with code. Thus, you are adding a function "frame2()" that places the Circle in the center of the stage each frame! You should instead place it at design time (link it to a property) into the second frame, or in a constructor, or you can use one single frame and Sprite instead of MovieClip, and instead of using frames you can use container sprites, adding and removing them at will, or at an action.
The other major mistake is adding an event listener inside an enterframe listener - these accumulate, not overwrite each other, so you can have multiple functions be designated as listeners for a particular event, or even one function several times. The latter happens for you, so each frame another instance of a listening keyHit function is added as a listener. The proper way to assign listeners is either in constructor, or in any function that listens for manually triggered event (say, MouseEvent.CLICK), but then you have to take precautions about listening for more than once with each function, and listening only with those functions you need right now.
EDIT:
Okay. Your code was:
addFrameScript(0, frame1);
addFrameScript(1, frame2);
The more correct way should be:
addFrameScript(0,frame1,1,frame2);
The reason is, the call to addFrameScript replaces all the timeline code with what you supply within here. The function is undocumented, perhaps by the reason of its affects on the stage and AS3 environment. The closest thing to the documentation on addFrameScript() so far is this link.
Next: Your code is:
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
x+=vx;
y+=vy
}
The correct way of writing this is as follows:
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
}
public function loop(e:Event) : void
{
x+=vx;
y+=vy
}
The listeners should be assigned in constructor, if they are permanent or you want them to be active as soon as you create an object. The KeyboardEvent listeners are separate case, as in order for them to function you have to assign them to stage, which is not available right at the time of creating the object, so you need an intermediate layer - the init() function, that is only called when the object is added to stage. At this point stage is no longer null, and you can assign an event listener there. Note, if you want to make your circles eventually disappear, you have to remove the listener you assigned to stage at some point of your removal handling code.
Next: Your code:
public function frame2()
{
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
Correct code should be:
public function frame2():void
{
if (Circle.parent) return; // we have added Circle to stage already!
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
See, you are calling this every time your MC is stopped at second frame, thus you constantly reset Circle's coordinates to stage center, so you just cannot see if it moves (it doesn't, as you have assigned the keyboard listener not to stage).
Perhaps there are more mistakes, but fixing these will make your MC tick a little bit.

AS3:Calling a switch case from another class does't work

i am making a game in flash and the problem that i am having is with two classes a class called Menu for the welcome screen which has a startgame button and the engine class which runs everything when i run the game for the first time i set the menu to appear automatically by this code:
public var state:int;
public const MENU:int = 0;
public const GAME:int = 1;
// create the variable for the menu
private var _menu:Menu;
public var sBg1:ScrollBg;
public var sBg2:ScrollBg;
public function Engine(menu:Menu)
{
_menu = menu;
//we add event listener when the engine is added to the stage
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
//then we remove the event listener and initiate the init function
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
init();
}
private function init():void {
//the init function set the game state at first to menu and run the drawUI function
state = MENU;
drawUI();
}
public function drawUI():void
{
//the drawUI function draw the needed elements on stage according to the state of the game
switch(state){
case MENU:
_menu = new Menu();
addChild(_menu);
break;
case GAME:
sBg1= new ScrollBg();
addChild(sBg1);
sBg1.x = 0;
sBg1.y = 0;
//if(contains(_menu)){
//removeChild(_menu);}
trace('this the game');
break;
}
}
and i change the state from menu to the actual game using this code in the menu class:
public var start_btn:MovieClip;
private var _engine:Engine;
public function Menu()
{
addEventListener(Event.ADDED_TO_STAGE, displayMenu);
}
private function displayMenu(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, displayMenu);
start_btn = new startBtn();
start_btn.x = 100;
start_btn.y = 200;
addChild(start_btn);
start_btn.addEventListener(MouseEvent.CLICK, startGame);
}
private function startGame(e:MouseEvent):void
{
start_btn.removeEventListener(MouseEvent.CLICK, startGame);
_engine = new Engine(this);
_engine.state = 1;
_engine.drawUI();
}
i use the drawUI function each time for the menu for example it places the start button and for the game it places the background but for some reason i only get the trace statement saying (this the game) but the background is not displayed any clues .
i spend more then 4 hours trying to find out what is the problem and i still can't if anyone can help me that would be great.
Thanks
If you see the trace but nothing on screen that is most likely because the ScrollBg class doesn't have anything to display. Make sure that class has some sprites or images in it.

create one addEventListener for all array elements in actionscript

I have an array of movie clips that represent buttons the user can click on, and so I need to use addEventListener function so the click can be handled.
I can use a loop and create an addEventListener for each element, I have 26 elements in the array, but I want to try another solution by using only one addEventListener and apply it on the array instead of the elements.
I want to know how to recognize which button was clicked, I mean what's its index in the array.
Thanks.
This is probably a good time to learn about event bubbling. You could just add one listener to the common parent of all your buttons
buttonContainer.addEventListener(MouseEvent.CLICK, buttonContainerClickHandler);
Then try and work out what was clicked
private function buttonContainerClickHandler(e:MouseEvent):void
{
var targetButton:Sprite = e.target as Sprite;
//do something with targetButton.
}
To find out what button was clicked, you could use the indexOf method of your array, and pass it the targetButton.
One thing you will have to do is make sure each of your buttons has mouseChildren set to false, or e.target will return the child assets of the buttons.
Add it to the movieclip. Adding an event listener to an array doesn't make a whole lot of sense. You are basically saying "Hey array, let me know when something about you changes" and Array isn't a subclass of EventDispatcher so that is a no-go. But in any case you don't want to know about the array, you want to know about the movieclip so the logical thing to do is to make the loop and add it to the movieclip.
You can't assign an event listener to an array.
What I think you're doing is applying a different event listener function to each clip in the array.
For each of the clips you can add the same event listener:
clips[i].addEventListener(MouseEvent.CLICK, handleClick);
and the handleClick function looks something like:
function handleClick(e:MouseEvent):void {
trace(clips.indexOf(e.target)) // outputs index the movieclip that was clicked on
}
Create a class for your buttons and add the event listener to the class. This way you don't even have to loop through your movie clips as the method will be part of the class
You can't get out of looping directly -- some loop will have to apply somewhere, but you can get out of looping indirectly -- you can let the VM loop for you. You should look at Array.forEach.
A simple application might be:
// assuming myArr is your array.
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, myClickHandler );
} );
To get the item's index, you might make something more complicated:
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, function( event:Event ):void
{
trace( "my index is", index );
} );
} );
I do have to recommend that you simply cache the array someplace accessible to your listener function and then use Array.indexOf along with event.currentTarget, but if you don't find that acceptable, you can use forEach this way:
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, function( event:Event ):void
{
trace( "my index is", array.indexOf( item ) );
} );
} );
Depending on how often you call those methods, it might not be faster to simply us this:
// probably not best to have this public
protected var myArr:Array = [/* whatever goes in here */]
public function register():void
{
myArr.forEach( addHandler );
}
protected function addHandler( item:IEventListener, index:int, arr:Array ):void
{
item.addEventListener( MouseEvent.CLICK, myClickHandler );
}
protected function myClickHandler( event:MouseEvent ):void
{
trace( event.currentTarget, "is at", myArr.indexOf( event.currentTarget ) );
}
But, I can't be sure without knowing more about your particular use case.
You could create your own custom vector class for IEventDispatcher objects that has a custom method that adds an event listener to all its elements. The best way to do this is to create a proxy class that acts as a wrapper for a Vector.<IEventDispatcher> object. I've created an example to demonstrate this:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.IEventDispatcher;
public class Main extends Sprite
{
private var _eventDispatcherVector:EventDispatcherVector;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var redCustomButton:CustomButton = new CustomButton("RED", 0xFF0000);
addChild(redCustomButton);
var blueCustomButton:CustomButton = new CustomButton("BLUE", 0x00FF00);
blueCustomButton.x = 100;
addChild(blueCustomButton);
var greenCustomButton:CustomButton = new CustomButton("GREEN", 0x0000FF);
greenCustomButton.x = 200;
addChild(greenCustomButton);
_eventDispatcherVector = new EventDispatcherVector(Vector.<IEventDispatcher>([redCustomButton,
blueCustomButton,
greenCustomButton]));
_eventDispatcherVector.addEventListener(MouseEvent.CLICK, onCustomButtonClick);
}// end function
private function onCustomButtonClick(e:Event):void
{
var customButton:CustomButton = e.target as CustomButton;
trace("You clicked: " + customButton.name + "\n" +
"Its index is: " + _eventDispatcherVector.indexOf(customButton));
}// end function
}// end class
}// end package
import flash.utils.Proxy;
import flash.utils.flash_proxy;
import flash.events.IEventDispatcher;
use namespace flash_proxy;
dynamic internal class EventDispatcherVector extends Proxy
{
private var _eventDispatcherVector:Vector.<IEventDispatcher>;
public function EventDispatcherVector(eventDispatcherVector:Vector.<IEventDispatcher>)
{
_eventDispatcherVector = eventDispatcherVector;
}// end function
override flash_proxy function getProperty(name:*):*
{
return _eventDispatcherVector[name];
}
override flash_proxy function setProperty(name:*, value:*):void
{
_eventDispatcherVector[name] = value;
}// end function
public function indexOf(searchElement:*, fromIndex:*= 0):int
{
return _eventDispatcherVector.indexOf(searchElement, fromIndex);
}// end function
public function addEventListener(type:String,
listener:Function,
useCapture:Boolean = false,
priority:int = 0,
useWeakReference:Boolean = false):void
{
for each(var eventDispatcher:IEventDispatcher in _eventDispatcherVector)
{
eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
}// end for each
}// end function
}// end class
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
internal class CustomButton extends Sprite
{
public function CustomButton(name:String, color:uint)
{
graphics.beginFill(color);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
var textField:TextField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.text = name;
textField.mouseEnabled = false;
textField.x = (100 / 2) - (textField.width / 2);
textField.y = (100 / 2) - (textField.height / 2);
addChild(textField);
this.name = name;
}// end function
}// end class
The output from clicking on the CustomButton objects is the following:
You clicked: RED
Its index is: 0
You clicked: BLUE
Its index is: 1
You clicked: GREEN
Its index is: 2
You can check out Stigglers answer for the question Actionscript 3.0 Best Option for Subclassing Vector Class (Flash Player 10) for more detail.