Copy link to clipboard
Copied
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!
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
...Copy link to clipboard
Copied
The event handler for the click should be able to remove the event listener for the button.
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
What is more important to you... to know which buttons were already clicked or to disable them when they are clicked?
Copy link to clipboard
Copied
Disable when they are clicked please!
Copy link to clipboard
Copied
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)
}
Copy link to clipboard
Copied
Thanks so much Ned! This code looks really neat and exactly what I want! Marked as correct answer!
Cheers!
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now