View Full Version : Can I assign an Instance Name to a Variable?
Betard
03-29-2008, 11:13 PM
Hey Everyone,
I have 3 MovieClips I am using for buttons, and for arguments sake their instance names are:
mc_BTN1
mc_BTN2
mc_BTN3
I have all my MouseEvent listeners set up and everything works perfect. but I would like to use a variable to hold their instance names... so let's call this variable 'myButton'
I am having using that variable with lines such as:
myButton.gotoAndStop("unselected");
myButton.mouseEnabled = true;
basically I want to be able to disable/enable buttons depending on what page they are on, and would like to pull the name of the last button pressed to a variable so I can enable it. (and naturally assign the name of the currently pressed button to that variable) so the next time through the function it knows what the last button pressed was.
Is there not a way for me to concatenate or in some way assign the button instance name with these types of functionality? I imagine their would be, but I am not familiar with all the "technical" terms for flash/AS3, so my attempts to search for 'AS3 variables' or 'Concatenate', etc... have not resulted in what I am after.
Is there a 'term' for what I am trying to do? (aside from 'assign a variable')
Any help is appreciated!
neznein9
03-30-2008, 12:31 AM
I'm a little confused about what you're trying to do.
It's important to remember that instance names are already variables. So you've already got an individualized handle on each of your buttons. If you want to keep one of those buttons as the active button (or whatever), you can use a second variable to point to the same MC:
var myButton:MovieClip;
myButton = mc_BTN1;
With that going, you can call whatever you want on myButton - this allows you to use the same code for each of the three buttons, just switch myButton to equal the new instance in between.
On the other hand, if you're trying to get one variable that holds all of your other clips, you'll want to use an array:
var myButton:Array = new Array();
myButton[0] = mc_BTN1;
myButton[1] = mc_BTN2;
myButton[2] = mc_BTN3;
Does that help at all? If not can you be more specific?
Betard
03-30-2008, 02:05 AM
Thanks for the reply neznein9, but I am not sure that is what I am after....
In my example below I have a function triggered by a MouseEvent listener, note in each "case" I have to gotoAndStop / mouseEnabled both of the other buttons...
So if "btnA" is clicked, the user is brought to a particular section of the website, the button is changed to its 'clicked on' appearance, and then de-activated (since there is no point in allowing a user to navigate to a page they are already on)
By putting "btnA" in the above mentioned state, we also need to make sure that any other buttons that were in that state are reset to their normal appearance and functionality so the user may navigate back to that section if desired.
Currently in each "case" below, I re-enable any buttons that are not teh one currently clicked... so if "btnA" is clicked, then I re-enable "btnB" and "btnC" even though only one of them would of been previously selected.
function onClickHandler1(myEvent:MouseEvent):void {
// if the clicked button is mouse enabled, stop the button on its
// 'selected' presentation state and then disable it
if (myEvent.target.mouseEnabled == true){
myEvent.target.gotoAndStop("mySelected");
myEvent.target.mouseEnabled = false;
}
switch (myEvent.currentTarget.name) {
case "btnA" :
// re-enable other buttons functions and presentation states
btnB.gotoAndStop("unselected");
btnB.mouseEnabled = true;
btnC.gotoAndStop("unselected");
btnC.mouseEnabled = true;
// do a bunch of stuff etc....
break;
case "btnB" :
// re-enable other buttons functions and presentation states
btnA.gotoAndStop("unselected");
btnA.mouseEnabled = true;
btnC.gotoAndStop("unselected");
btnC.mouseEnabled = true;
// do a bunch of stuff etc....
break;
case "btnC" :
// re-enable other buttons functions and presentation states
btnA.gotoAndStop("unselected");
btnA.mouseEnabled = true;
btnB.gotoAndStop("unselected");
btnB.mouseEnabled = true;
// do a bunch of stuff etc....
break;
}
}
IF I was simply able to make a "btnPrevSelected" variable, than instead of re-enabling all other buttons I could simply track what the last selected button was, and then re-enable that one....
I know the code below doesn't work, but is here just to give you an idea. So my "case" actions would function more like this:
switch (myEvent.currentTarget.name) {
case "btnA" :
// re-enable last selected button button functions and presentation state
btnPrevSelected.gotoAndStop("unselected");
btnPrevSelected.mouseEnabled = true;
// do a bunch of stuff etc....
// after navigation etc... takes place, assign this button
// to the btnPrevSelected variable so next button is clicked
// will know this was the last button selected.
btnPrevSelected = the.button.that.was.just.selected;
break;
case "btnB" :
// etc......
}
}
Hopefully this paints a better visual as to what I am trying to do.
neznein9
03-30-2008, 02:56 AM
I see what you're after now. What you need is a class-scoped variable...ie. one that exists outside a function so it will be persistent from one function-call to the next. So outside of your handler function throw this in:
var btnPrevSelected:MovieClip;
Then inside your handler you can reset the previous button and when you're done set the new button into the variable:
btnPrevSelected.gotoAndStop("unselected");
btnPrevSelected.mouseEnabled = true;
btnPrevSelected = myEvent.currentTarget;
So between that and myEvent.currentTarget, you can eliminate your switch statement altogether...one little snag though, you will probably get errors the first time you run that code because the first time through there won't be anything selected...you could do a big if...then to test if it's undefined but it's probably easier to just set one of your buttons as btnPrevSelected even though it's not selected yet.
One last optimization - unless you have a specific reason, you should try using buttonMode = true/false instead of mouseEnabled. It will give you the same results but when you click on something thats buttonMode=false, it won't call the function, which means you don't have to do the if...then check for it at the beginning. Hope this helps!
Betard
03-30-2008, 03:10 AM
It seems like this is really close... but when I test my file it simply loops over and over all my animations... and I get the following error:
1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:MovieClip.
which is my line where I have:
btnPrevSelected = myEvent.currentTarget;
seems it doesn't like that at all.
Betard
03-30-2008, 03:43 AM
Awesome neznein9 you practically nailed it... with a little help from google I was able to do the adjustment to your code that were needed. I am adding the working fix here incase anyone else comes across this thread.
Hopefully it can be of some help to others as well.
var btnPrevSelected:MovieClip = new MovieClip();
Then inside your handler you can reset the previous button and when you're done set the new button into the variable:
btnPrevSelected.gotoAndStop("unselected");
btnPrevSelected.mouseEnabled = true;
btnPrevSelected = MovieClip(myEvent.currentTarget);
Thanks again neznein9!!
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.