FORUMS: list search recent posts

Getting Names from Arrayed Buttons

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Sam Pipes
Getting Names from Arrayed Buttons
on Feb 6, 2012 at 1:37:47 pm

I'm a fresh convert from AS2 to AS3, and I'm beginning to suspect my previous experience isn't going to help me at all. I'm making a very simple app, just ten buttons, all with similar names, and I'm using a for loop to do them all at once. So far so good, as far as I can tell. My problem comes when I try to identify which button was clicked.

Here's my code so far:
import flash.events.MouseEvent;

var i:int;
var j:int;
var curClick:String = "";

onOpen();

function onOpen():void{
for (i = 1; i <= 10; i++)
{
this["dice"+i].numbox.text = "";
this["dice"+i].buttonMode = true;
this["dice"+i].glow.alpha = 0;
curClick = this["dice"+i].name;
this["dice"+i].addEventListener (MouseEvent.CLICK, onClick);
}
}


function onClick ($event:MouseEvent) {
trace(curClick);
}


The result of which is that every button returns "dice10." I feel like if I could just get this working the rest would fall into place.

Also is there any website or forum you guys would recommend for people moving from AS2 to AS3?


Return to posts index

demetri tashie
Re: Getting Names from Arrayed Buttons
on Feb 6, 2012 at 3:51:16 pm

sam,

your 'for' loop is starting at (var i =1) and ending at (var i=10), changing curClick each time.
the last time it sets curClick to be 'dice10', so that is what it will remain until it is further changed, which clicking on a button will not do with your code.

what i believe you want to do is to return ( trace out) the instance name of the button that is clicked. so it can be done by asking for the event target's name, usually done like this:
event.target.name or the way i prefer e.target.name
since you use the custom event name '$event', your onClick function would work like this:
function onClick ($event:MouseEvent) {
trace($event.target.name);
}


so basically your variable curClick was useless as it was fixed at 'dice10' with no means to change.

a possible way to make it useful for some other situations is to not declare it in the 'for' loop, but in the 'onClick' function to declare it as such:
curClick = $event.target.name;
then you would have a record of the last button clicked, which could be useful in some situations.

i hope that helps. switching from AS2 to AS3 is a little bit of an old subject right now, so the forums for that have died down. pick up a good beginning book on AS3. if you have a good handle on AS2, it should go pretty quick for you.


Return to posts index

demetri tashie
Re: Getting Names from Arrayed Buttons (working exammple)
on Feb 6, 2012 at 4:41:32 pm

if you want to see this in action, consider this complete set of working code, for 10 movieclip symbols on the stage with names 'dice1' through 'dice10'

this is an example based on your initial code, for something that i get asked about all the time, that of making 'sticky' buttons to show which button was the last one clicked. that is one situation where this variable 'curClick' would come in useful:

var curClick:String = "";
this[curClick]=dice4; // picked arbitrarily - to initialize this[curClick] to any movieclip

onOpen();
function onOpen():void{
for (var i:uint = 1; i <= 10; i++)
{
this["dice"+i].buttonMode = true;
this["dice"+i].alpha = .5; //sets inital alpha of all buttons to .5
this["dice"+i].addEventListener (MouseEvent.CLICK, onClick);
}
}

function onClick ($event:MouseEvent) {
trace($event.target.name);
this[curClick].alpha= .5; // returns current button to original alpha (.5)
this[$event.target.name].alpha=1; // changes button clicked to alpha = 1
this[curClick] = this[$event.target.name]; // assigns curClick to be the last button clicked
}


i hope this helps further


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]