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

How to use the same script for multiple buttons

New Here ,
Aug 15, 2013 Aug 15, 2013

Hi,

I've only just started using flash so any help would be great!

I'm creating a blockbusters type game, I have a grid of 20 buttons and I need  them (individually) to turn blue on click and red on double click. I've managed to do it with the first one using this code;

--------------------------------------------

var clicked:Boolean = false;

bn1.addEventListener(MouseEvent.CLICK, bn1click);

function bn1click(event:MouseEvent):void {

    clicked = true;

    var newColorTransform:ColorTransform = bn1.transform.colorTransform;

    if(clicked){

    newColorTransform.color = 0x064258;

    }

    bn1.transform.colorTransform = newColorTransform;

}

/////////////

bn1.doubleClickEnabled = true;

var doubleclicked:Boolean = false;

bn1.addEventListener(MouseEvent.DOUBLE_CLICK, bn1dclick);

function bn1dclick(event:MouseEvent):void {

    doubleclicked = true;

    var newColorTransform:ColorTransform = bn1.transform.colorTransform;

    if(clicked){

    newColorTransform.color = 0xac1e23;

    }

    bn1.transform.colorTransform = newColorTransform;

}

--------------------------------------------

Now I'm having trouble getting the same to work for the rest of the buttons, they are each named bn2, bn3 etc. They need to work individually and remain blue/red once clicked. I've tried listing them as addEventListener commands but not having a great deal of success!

Any help would be greatly appreciated, thanks!

Tomo

TOPICS
ActionScript
1.4K
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 , Aug 15, 2013 Aug 15, 2013

One way to do this is to use arrays to keep track of the buttons and their properties.

var buttonList:Array = new Array(bn1,bn2,bn3);

var clickedList:Array = new Array();

var doubleClickedList:Array = new Array();

//then use a for loop to assign the functions and properties for each button:

 

var thisMany:int = buttonList.length; // this will give you the number of items in the buttonList array

for(var i:int = 0; i<thisMany; i++) {

     buttonList.addEventListener(MouseEvent.CLICK,btnClick); // assign

...
Translate
LEGEND ,
Aug 15, 2013 Aug 15, 2013

If you create them as movieclips you can dynamically assign the clicked and doubleclicked properties to each - if that is the intention (Meaning each button should have its own clicked/doubleclicked property). 

To have the same functions shared by all the buttons you can use the event.currentTarget (which will pont to the button that was clicked).  For example...

// this loop will assign listeners to all 20 buttons

for(var i:int=1; i<21; i++){

      this["bn"+String(i)].addEventListener(MouseEvent.CLICK, bnclick);

}

function bnclick(event:MouseEvent):void {

    MovieClip(event.currentTarget).clicked = true;

    var newColorTransform:ColorTransform = MovieClip(event.currentTarget).transform.colorTransform;

    if(MovieClip(event.currentTarget).clicked){

         newColorTransform.color = 0x064258;

    }

    MovieClip(event.currentTarget).transform.colorTransform = newColorTransform;

}

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
New Here ,
Aug 15, 2013 Aug 15, 2013

Hi, thanks for the quick reply!

This works perfectly for the single click, I've added the double click and can't seem to make it work...this was my best guess for the double click but the 1st line doesn't work;

------------------

["mc"+String(i)].doubleClickEnabled = true;

var doubleclicked:Boolean = false;

{

this["mc"+String(i)].addEventListener(MouseEvent.DOUBLE_CLICK, mcdclick);

}

function mcdclick(event:MouseEvent):void {

   

    MovieClip(event.currentTarget).doubleclicked = true;

   

    var newColorTransform:ColorTransform = MovieClip(event.currentTarget).transform.colorTransform;

   

    if(MovieClip(event.currentTarget).clicked){

       

        newColorTransform.color = 0xac1e23;

       

    }

   

    MovieClip(event.currentTarget).transform.colorTransform = newColorTransform;

   

}

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 ,
Aug 15, 2013 Aug 15, 2013

One way to do this is to use arrays to keep track of the buttons and their properties.

var buttonList:Array = new Array(bn1,bn2,bn3);

var clickedList:Array = new Array();

var doubleClickedList:Array = new Array();

//then use a for loop to assign the functions and properties for each button:

 

var thisMany:int = buttonList.length; // this will give you the number of items in the buttonList array

for(var i:int = 0; i<thisMany; i++) {

     buttonList.addEventListener(MouseEvent.CLICK,btnClick); // assign the click function to each button

     buttonList.addEventListener(MouseEvent.DOUBLE_CLICK,btnDClick); // assign the double click function

     clickedList.push(false);  // add a false value for each button to this array

     doubleClickedList.push(false);

           buttonList.doubleClickEnabled = true; // set the double click property for each button

}

function btnClick(event:MouseEvent):void {

          var thisButton:int = buttonList.indexOf(event.target);  // figure out which button was clicked as an item in the array

          clickedList[thisButton] = true;  // change the value in the array

           var newColorTransform:ColorTransform = buttonList[thisButton].transform.colorTransform;

    if(clickedList[thisButton]){

    newColorTransform.color = 0x064258;

    }

    buttonList[thisButton].transform.colorTransform = newColorTransform;

}

function btnDClick(event:MouseEvent):void {

          var thisButton:int = buttonList.indexOf(event.target);

          doubleClickedList[thisButton] = true;

    var newColorTransform:ColorTransform = buttonList[thisButton].transform.colorTransform;

    if(doubleClickedList[thisButton]){

    newColorTransform.color = 0xac1e23;

    }

    buttonList[thisButton].transform.colorTransform = newColorTransform;

}

Now you can have any number of buttons, just add their instance names to the array at the top.

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
New Here ,
Aug 15, 2013 Aug 15, 2013

Thanks Rob, I will try this as well!

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
New Here ,
Aug 15, 2013 Aug 15, 2013
LATEST

Yes this works perfectly Thank you!

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