Skip to main content
JasonBrownCC2014
Inspiring
September 30, 2015
Answered

Is there a way to detect all of the buttons that have been clicked in AS3?

  • September 30, 2015
  • 1 reply
  • 523 views

Hi all,

Quick question - is there a way to detect all of the buttons that have been clicked using AS3 code? I have buttons on my project and I want them to be disabled once the user has clicked on them, but I wonder if there's a function I can write that will automatically disable all of the buttons that have been clicked once?

Thanks!

This topic has been closed for replies.
Correct answer Ned Murphy

Disable when they are clicked please!


Here is an example where the buttons share the same event handler... try it and see if it works (I can't due to not having Flash at hand).  The only problem I might expect is it not liking the evt.currentTarget not being  identifiable as a particular class type, but sometimes it doesn't happen.  If it does there is an easy fix.

btn1.addEventListener(MouseEvent.CLICK, processBtnClick);

btn2.addEventListener(MouseEvent.CLICK, processBtnClick);

function processBtnClick(evt:MouseEvent):void {

       evt.currentTarget.removeEventListener(MouseEvent.CLICK, processBtnClick)

}

if you want to also keep track of which buttons were clicked already you can store them in an array....

var clickedButtons:Array = new Array();

btn1.addEventListener(MouseEvent.CLICK, processBtnClick);

btn2.addEventListener(MouseEvent.CLICK, processBtnClick);

function processBtnClick(evt:MouseEvent):void {

       evt.currentTarget.removeEventListener(MouseEvent.CLICK, processBtnClick)

       clickedButtons.push(evt.currentTarget)

}

1 reply

Ned Murphy
Legend
September 30, 2015

The event handler for the click should be able to remove the event listener for the button.

JasonBrownCC2014
Inspiring
September 30, 2015

Thanks for your speedy response Ned! Would it be possible to give me an example of some code I could use to detect all of the buttons that have been pressed? I'm still new to this! Thanks!

Ned Murphy
Legend
September 30, 2015

What is more important to you... to know which buttons were already clicked  or to disable them when they are clicked?