Copy link to clipboard
Copied
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
1 Correct answer
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
...Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks Rob, I will try this as well!
Copy link to clipboard
Copied
Yes this works perfectly Thank you!

