Modify a variable when an event is triggered in AS3 - actionscript-3

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.

Related

Actionscript 3 Call to a possibly undefined method

Here is the problem, the object is moved together with the clicked object. I want it to be moveable following the mouse pointer, but let the clicked object stays. so when an object is clicked, there will be 2 objects in the stage(the static and moving one).
I think I've figured it out by adding a new object to be moved. in function onClickHero I've tried movingHero = new heroes but it says "call to a possibly undefined method heroes". My question is there any other way how to make another clone of the clicked object since I made it in array? And why does movingHero = new heroes doesn't work?
I'm still amateur at classes. Sorry if it's messed up. Thanks for helping.
package {
import flash.display.MovieClip
import flash.events.MouseEvent
import flash.events.Event
import flash.display.Sprite
public class Hero {
private var heroesArray:Array;
private var heroContainer:Sprite = new Sprite;
private var hero1:MovieClip = new Hero1();
private var hero2:MovieClip = new Hero2();
private var moveHero:Boolean = false;
private var movingHero:MovieClip;
private var _money:Money = new Money();
private var _main:Main;
public function Hero(main:Main)
{ _main = main;
heroesArray = [hero1,hero2];
heroesArray.forEach(addHero);
}
public function addHero(heroes:MovieClip,index:int,array:Array):void
{
heroes.addEventListener(Event.ENTER_FRAME, playerMoving);
heroes.addEventListener(MouseEvent.CLICK, chooseHero);
}
public function playerMoving(e:Event):void
{
if (moveHero == true)
{
movingHero.x = _main.mouseX;
movingHero.y = _main.mouseY;
}
}
public function chooseHero(e:MouseEvent):void
{
var heroClicked:MovieClip = e.currentTarget as MovieClip;
var cost:int = _main._money.money ;
if(cost >= 10 && moveHero == false)
{
_main._money.money -= 10;
_main._money.addText(_main);
onClickHero(heroClicked);
moveHero = true;
}
}
public function onClickHero(heroes:MovieClip):void
{
movingHero = heroes;
heroContainer.addChild(movingHero);
}
public function displayHero(stage:Object):void
{
stage.addChild(heroContainer);
for (var i:int = 0; i<2;i++)
{
stage.addChild(heroesArray[i]);
heroesArray[i].x = 37;
heroesArray[i].y = 80+i*70;
heroesArray[i].width=60;
heroesArray[i].height=55;
heroesArray[i].buttonMode = true;
}
}
}
}
EDIT: I've tried to make movingHero = new Hero1(); but since I don't know which hero will be clicked so I can't just use Hero1 from library. and If I use movingHero = heroClicked I only get the value of hero1 which is a var from Hero1 movieclip. So, is there any way to call the movie clip from library the same as which hero was clicked in stage?
You seemingly want to clone an object while not knowing its type. If that object also containg game logic, it's not the best idea to say spawn new heroes of either type, this might make a mess of your code. But if not, you can get the exact class of the object given, and make an object of that class via the following code:
public function onClickHero(heroes:MovieClip):void
{
if (!heroes) {
trace('heroes is null!');
return;
}
var heroClass:Class = getDefinitionByName(getQualifiedClassName(heroes)) as Class;
movingHero = new heroClass(); // instantiate that class
heroContainer.addChild(movingHero);
// movingHero.startDrag(); if needed
}
Don't forget to clean up the movingHero once it's no longer needed.

AS3 Scrolling Movieclip

I have a problem with my code whereby I have a spawned vertical movieclip where there are button instances in it which makes it look like a list.
The problem is that I am trying to recreate a iOS-style drag scrolling. When I hold and drag my movieclip, it works fine, scrolling up and down. But when I release my left mouse button, It registers a click on one of my buttons.
I tried to remove the event listeners on the buttons but the scrolling only works once, after I released my left mouse button, the whole scrolling does not work the second time.
Is it possible to have maybe like a mouse drag (up or down) ignoring the button click listeners however when the user want to click the button instead, the scrolling won't kick in?
My buttons' class
public function PlaceOneButtons()
{
for (var a=0; a<buttons.length; a++)
{
stationsone1[a].addEventListener(clicked,StationSelectOne);
stationsone2[a].addEventListener(clicked,StationSelectOne);
stationsone3[a].addEventListener(clicked,StationSelectOne);
stationsone4[a].addEventListener(clicked,StationSelectOne);
stationsone5[a].addEventListener(clicked,StationSelectOne);
}
}
My Main (spawner) class
package
{
import flash.display.MovieClip;
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
import flash.media.*;
import IconSpawn;
import Scrolling;
public class MainClass extends MovieClip
{
private var iconspawn:IconSpawn = new IconSpawn();
private var touchay:int = new int;
private var touchPoint:int = new int;
private var touchPoint2:int = new int;
private var touchString:int = new int;
private var AYint:int = new int;
private var touchTimer:Timer = new Timer(150,1);
private var endTime:Timer = new Timer(1,1);
private var speed:int = new int;
public static var scrollDiff:int = new int;
public static var doubleDiff:int = new int;
public static var dragging:Boolean = new Boolean
//private var scrolling:Scrolling = new Scrolling();
//public static var Ystore:Point;
public function MainClass()
{
// constructor code
AYint = IconSpawn.A_Y.y;
}
public function startApp()
{
addChild(iconspawn);
iconspawn.MenuSpawn();
dragging = false;
}
public function directionsApp()
{
addChild(iconspawn);
iconspawn.KeyboardOne();
}
public function placeOneApp()
{
addChild(iconspawn);
iconspawn.PlaceOneSpawn();
Evtlistener();
}
private function Evtlistener()
{
addEventListener(Event.ENTER_FRAME,update);
addEventListener(MouseEvent.MOUSE_DOWN,spawnTouch);
addEventListener(MouseEvent.MOUSE_UP,endScroll);
}
public function directionsApp2()
{
addChild(iconspawn);
iconspawn.KeyboardTwo();
}
public function update(evt:Event)
{
//trace(touchTimer);
//trace(touchString);
touchPoint2 = mouseY;
scrollDiff = touchPoint2 - touchPoint;
doubleDiff = scrollDiff - scrollDiff;
trace(dragging);
if(dragging == true)
{
//iconspawn.PlaceOneButtons();
}
}
public function spawnTouch(evt:MouseEvent)
{
touchPoint = mouseY;
touchTimer.addEventListener(TimerEvent.TIMER,timerTouch);
endTime.addEventListener(TimerEvent.TIMER,endTimer);
touchTimer.start();
dragging = true;
touchay = IconSpawn.A_Y.y;
}
public function timerTouch(evt:TimerEvent):void
{
if(dragging == true)
{
addEventListener(Event.ENTER_FRAME,startScroll);
}
}
public function startScroll(evt:Event)
{
if(mouseY > 540 && mouseY < 1510)
{
IconSpawn.A_Y.y = touchay + (touchPoint2 - touchPoint);
}
}
public function endScroll(evt:MouseEvent)
{
removeEventListener(MouseEvent.MOUSE_DOWN,spawnTouch);
removeEventListener(Event.ENTER_FRAME,startScroll);
touchTimer.reset();
endTime.start();
}
private function endTimer(evt:TimerEvent):void
{
dragging = false;
Evtlistener();
}
}
}
Help will be appreciated! Have been stuck at this problem for days now.
UPDATE: Added new updated code
Hi guys, now my problem is that after I scroll my movieclip, the code will listen out for scrollDiff if its = 0.
When scrollDiff is 0, the buttons of the movieclip is clickable but if its more or less than that, the buttons are not clickable. Now my new problem is that after I release my left click, the code does not update the scrollDiff to 0, so user have to double click to select the button. Help!
package
{
import flash.display.MovieClip;
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
import flash.media.*;
import IconSpawn;
import Scrolling;
public class MainClass extends MovieClip
{
private var iconspawn:IconSpawn = new IconSpawn();
private var touchPoint:int = new int;
private var touchPoint2:int = new int;
private var AYint:int = new int;
private var touchTimer:Timer = new Timer(150,1);
private var endTimer:Timer = new Timer(150,1);
private var IconSpwnY:int = new int;
private var touchbool:Boolean = new Boolean
public static var scrollDiff:int = new int;
//private var scrolling:Scrolling = new Scrolling();
//public static var Ystore:Point;
public function MainClass()
{
// constructor code
}
public function startApp()
{
addChild(iconspawn);
iconspawn.MenuSpawn();
}
public function directionsApp()
{
addChild(iconspawn);
iconspawn.KeyboardOne();
}
public function placeOneApp()
{
AYint = IconSpawn.A_Y.y;
addChild(iconspawn);
iconspawn.PlaceOneSpawn();
addEventListener(Event.ENTER_FRAME,update);
addEventListener(MouseEvent.MOUSE_DOWN,spawnTouch);
addEventListener(MouseEvent.MOUSE_UP,endScroll);
touchbool = false;
//IconSpawn.container.mouseChildren = true;
}
public function directionsApp2()
{
addChild(iconspawn);
iconspawn.KeyboardTwo();
}
public function update(evt:Event)
{
touchPoint2 = mouseY;
scrollDiff = touchPoint2 - touchPoint;
trace(scrollDiff);
}
public function spawnTouch(evt:MouseEvent)
{
touchPoint = mouseY;
touchTimer.addEventListener(TimerEvent.TIMER,timerTouch);
touchTimer.start();
IconSpwnY = IconSpawn.A_Y.y;
if(scrollDiff == 0)
{
IconSpawn.container.mouseChildren = true; //Important <-
}
else
{
IconSpawn.container.mouseChildren = false; //Important <-
}
}
public function timerTouch(evt:TimerEvent):void
{
addEventListener(Event.ENTER_FRAME,startScroll);
touchbool = true
}
public function startScroll(evt:Event)
{
if(mouseY > 0 && mouseY < 1510)
{
IconSpawn.A_Y.y = IconSpwnY + (touchPoint2 - touchPoint);
}
}
public function endScroll(evt:MouseEvent)
{
removeEventListener(MouseEvent.MOUSE_DOWN,spawnTouch);
removeEventListener(Event.ENTER_FRAME,startScroll);
endTimer.addEventListener(TimerEvent.TIMER,endTiming);
endTimer.start();
trace("CALLL")
scrollDiff = touchPoint2 - touchPoint;
//touchTimer.reset();
}
private function endTiming(evt:TimerEvent)
{
IconSpawn.container.mouseChildren = true;
addEventListener(MouseEvent.MOUSE_DOWN,spawnTouch);
//addEventListener(Event.ENTER_FRAME,startScroll);
scrollDiff = 0;
}
}
}
While you're moving set <container_mc>.mouseChildren = false. When the container clip stops moving, set the mouseChildren property to 'true'.
Adding to Craig's answer (which I definitely recommend implementing, to make life easier!), here is the list of the event listeners you should have hooked up for this to work as planned. This is theory, so you may have to play around with it a bit.
container.addEventListener(MouseEvent.MOUSE_DOWN, response);: You'll want to raise some sort of flag that the mouse is down, and wait for the next mouse event, which determines the action.
container.addEventListener(MouseEvent.MOUSE_MOVE, response);: If the mouse is down, disable child clicking (Craig's answer) and initiate dragging. You'll also want to raise a dragging flag.
container.addEventListener(MouseEvent.MOUSE_UP, response);: If the dragging flag is raised, stop drag and enable child clicking. If it isn't raised, do nothing, so the item can take over from there.
item.addEventListener(MouseEvent.MOUSE_UP, itemResponse);: Your click action.
Theoretically, this should work fine with your nested MovieClips, as long as you use MouseEvent.MOUSE_UP as the listener on them (as shown above). Using MouseEvent.MOUSE_DOWN or MouseEvent.CLICK may mess your timing up. However, I'd experiment a bit to find the right combination, as I could be wrong on that last point.

How to change cursor on Roll Over event

I'm making a point and click game in AS3 with flash.
I've changed the skin of my cursor by creating a new class "Souris". It's working just fine. Now I'm trying to change the skin of the cursor when it's on an object on the scene.
I've read that the MouseEvent.ROLL_OVER is the good method but I can't figure out how to do it...
I've got my Souris class like that :
public class Souris extends MovieClip
{
private var engine:Engine;
private var stageRef:Stage;
private var p:Point = new Point();
public function Souris(stageRef:Stage)
{
Mouse.hide(); //make the mouse disappear
mouseEnabled = false; //don't let our cursor block anything
mouseChildren = false;
this.stageRef = stageRef;
x = stageRef.mouseX;
y = stageRef.mouseY;
stageRef.addEventListener(MouseEvent.MOUSE_MOVE, updateMouse, false, 0, true);
stageRef.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler, false, 0, true);
stageRef.addEventListener(Event.ADDED, updateStack, false, 0, true);
stageRef.addEventListener(MouseEvent.ROLL_OVER,hover);
}
private function updateStack(e:Event) : void
{
stageRef.addChild(this);
}
private function hover(e:MouseEvent):void {
souris.visible = false;
}
private function mouseLeaveHandler(e:Event) : void
{
visible = false;
Mouse.show(); //in case of right click
stageRef.addEventListener(MouseEvent.MOUSE_MOVE, mouseReturnHandler, false, 0, true);
}
private function mouseReturnHandler(e:Event) : void
{
visible = true;
Mouse.hide(); //in case of right click
removeEventListener(MouseEvent.MOUSE_MOVE, mouseReturnHandler);
}
private function updateMouse(e:MouseEvent) : void
{
x = stageRef.mouseX;
y = stageRef.mouseY;
e.updateAfterEvent();
}
}
}
}
In my main class (Engine class) I've got :
private var souris:Souris;
public function Engine(){
souris = new Souris(stage);
stage.addChild(souris);
}
private function startGame(e:Event):void{
....
..
I've tried to put in the "Souris" class
stageRef.addEventListener(MouseEvent.ROLL_OVER,hover);
private function hover(e:MouseEvent):void {
Engine.souris.visible = false;
handCursor.visible = true ;
}
But it seems wrong...
I don't know what to put in my hover function. (I've got the "handCursor" in my library).
Thank you very much for your help!
If you have "handCursor" in your library, then you need to assign a class to it, like 'HandCursor'. I advise that classes start with an uppercase letter.
So your code would need to create a new instance of it then show it, like
var handCursor:HandCursor = new HandCursor; handCursor.visible = false;
handCursor.visible = false; makes it not visible, then to make it visible, you would:
handCursor.visible = true;
Also, handCursor is a local variable if put in a function, to make it global to use in all functions, you would need to put it at the beginning of your Class.
Also, are you getting any errors? If so, please share them.

AS3: Error 1009: Null reference

I am making a game in ActionScript 3. I have a Menu Class with a method that renders a Menu.
I make an instance of Menu in my Main class, and then call the method. When I debug the application I get a null reference error. This is the code of the menu class:
package
{
import flash.display.MovieClip;
import menucomponents.*;
public class Menu extends MovieClip
{
public function Menu()
{
super();
}
public function initMenuComponents():void{
var arrMenuButtons:Array = new Array();
var btnPlay:MovieClip = new Play();
var btnOptions:MovieClip = new Options();
var btnLikeOnFacebbook:MovieClip = new LikeOnFacebook();
var btnShareOnFacebook:MovieClip = new ShareOnFacebook()
arrMenuButtons.push(btnPlay);
arrMenuButtons.push(btnOptions);
arrMenuButtons.push(btnLikeOnFacebbook);
arrMenuButtons.push(btnShareOnFacebook);
var i:int = 0;
for each(var item in arrMenuButtons){
item.x = (stage.stageWidth / 2) - (item.width / 2);
item.y = 100 + i*50;
item.buttonMode = true;
i++;
}
}
}
}
Thanks in advance.
Your issue is likely that stage is not populated yet when your for loop runs. Try the following:
public class Main extends MovieClip {
public function Main() {
super();
var menu:Menu = new Menu();
//it's good practice - as sometimes stage actually isn't populated yet when your main constructor runs - to check, though in FlashPro i've never actually encountered this (flex/flashBuilder I have)
if(stage){
addedToStage(null);
}else{
//stage isn't ready, lets wait until it is
this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
}
}
private function addedToStage(e:Event):void {
menu.addEventListener(Event.ADDED_TO_STAGE,menuAdded); //this will ensure that stage is available in the menu instance when menuAdded is called.
stage.addChild(menu);
}
private function menuAdded(e:Event):void {
menu.initMenuComponents();
}
}

AS3 question - Best way to lockout buttons

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.