Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Sep 30, 2015 Sep 30, 2015

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!

TOPICS
ActionScript
451
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 30, 2015 Sep 30, 2015

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

...
Translate
LEGEND ,
Sep 30, 2015 Sep 30, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 30, 2015 Sep 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2015 Sep 30, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 30, 2015 Sep 30, 2015

Disable when they are clicked please!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2015 Sep 30, 2015

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)

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 30, 2015 Sep 30, 2015

Thanks so much Ned! This code looks really neat and exactly what I want! Marked as correct answer!

Cheers!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2015 Sep 30, 2015
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines