PDA

View Full Version : Using Variables to Create Dynamic Button Instance Names


Scottiedazzle
07-26-2005, 09:17 PM
Help! :eek:

I am at my wit's end here. The following code works like a charm for creating a variable from the positition of the current frame, turning that variable into the name of a movie clip, then applying a function to that movie clip (in this case, loading text into a TextArea component).


textloader = function () {
var holeidentity = Math.floor((_currentframe + 3)/4); //rounds product down to determine hole number
var holestring:String= "0" + holeidentity;//turn holeidentity into a string
if (holestring.length == 2) {
var currenthole = holestring.substr(0, 2);//pulls 2 digit string out of 2 digit number
} else {
var currenthole = holestring.substr(1, 2);//pulls 2 digit string out of 3 digit number
}

//code for loading text deleted
}
var holeidentity = Math.floor((_currentframe + 3)/4); //rounds product down to determine hole number
var holestring:String= "0" + holeidentity;//turn holeidentity into a string
if (holestring.length == 2) {
var currenthole = holestring.substr(0, 2);//pulls 2 digit string out of 2 digit number
} else {
var currenthole = holestring.substr(1, 2);//pulls 2 digit string out of 3 digit number
}
if (((_currentframe/4)-holeidentity) == .25) {
var currentlocation = "tee";
} else if (((_currentframe/4)-holeidentity) == -.75) {
var currentlocation = "tee";
} else if (((_currentframe/4)-holeidentity) == .5) {
var currentlocation = "fairwaya";
} else if (((_currentframe/4)-holeidentity) == -.5) {
var currentlocation = "fairwaya";
}else if (((_currentframe/4)-holeidentity) == .75) {
var currentlocation = "fairwayb";
} else if (((_currentframe/4)-holeidentity) == -.25) {
var currentlocation = "fairwayb";
} else {
var currentlocation = "green"
}

currenthole_mc = "hole" + currenthole + currentlocation + "1_mc";
textloader.apply(currenthole_mc);

This formula will figure out where it is in the timeline, accordingly determine what text to load, then load the text. It even figures out what movie clip instance it is being applied to, so it will load the right text. :)

So, why won't this work for a button action on the main timeline? :( The only difference that I see between the two functions is that the variable currentbutton, which is determined the same way the variable currenthole_mc is in the above example, cannot have an action (onRollOver) applied to it.

interactivebtnA= function () {
var holeidentity = Math.floor((_currentframe + 3)/4); //rounds product down to determine hole number
var holestring:String= "0" + holeidentity;//turn holeidentity into a string
if (holestring.length == 2) {
var currenthole = holestring.substr(0, 2);//pulls 2 digit string out of 2 digit number
} else {
var currenthole = holestring.substr(1, 2);//pulls 2 digit string out of 3 digit number
}
if (((_currentframe/4)-holeidentity) == .25) {
var currentlocation = "tee";
} else if (((_currentframe/4)-holeidentity) == -.75) {
var currentlocation = "tee";
} else if (((_currentframe/4)-holeidentity) == .5) {
var currentlocation = "fairwaya";
} else if (((_currentframe/4)-holeidentity) == -.5) {
var currentlocation = "fairwaya";
}else if (((_currentframe/4)-holeidentity) == .75) {
var currentlocation = "fairwayb";
} else if (((_currentframe/4)-holeidentity) == -.25) {
var currentlocation = "fairwayb";
} else {
var currentlocation = "green"
}

var currentbutton = "hole" + currenthole + currentlocation + "1_btn"

currentbutton.onRollOver = function () {
//The function has trouble calculating what currentbutton is!!
}

currentbutton.onRollOut = function () {
//The function has trouble calculating what currentbutton is!!
}
}

var holeidentity = Math.floor((_currentframe + 3)/4); //rounds product down to determine hole number
var holestring:String= "0" + holeidentity;//turn holeidentity into a string
if (holestring.length == 2) {
var currenthole = holestring.substr(0, 2);//pulls 2 digit string out of 2 digit number
} else {
var currenthole = holestring.substr(1, 2);//pulls 2 digit string out of 3 digit number
}
if (((_currentframe/4)-holeidentity) == .25) {
var currentlocation = "tee";
} else if (((_currentframe/4)-holeidentity) == -.75) {
var currentlocation = "tee";
} else if (((_currentframe/4)-holeidentity) == .5) {
var currentlocation = "fairwaya";
} else if (((_currentframe/4)-holeidentity) == -.5) {
var currentlocation = "fairwaya";
}else if (((_currentframe/4)-holeidentity) == .75) {
var currentlocation = "fairwayb";
} else if (((_currentframe/4)-holeidentity) == -.25) {
var currentlocation = "fairwayb";
} else {
var currentlocation = "green"
}

var currentholeA_btn = "hole" + currenthole + currentlocation + "1_btn"
interactivebtnA.apply(currentholeA_btn);

Is there something simple that I am missing? I am still learning arrays and such, so the use of variables is the best solution that I can think of to tell a function which instance of a button the actions should be applied to.

Any help would be greatly appreciated! :o

Ricod
07-26-2005, 09:28 PM
I'm still wading through it. But in the meantime, may I suggest you stop using if else constructions like that ? The Switch () method is tidier :

Switch ((_currentframe/4)-holeidentity) {
case .25 :
var currentlocation = "tee";
break;
case -.75 :
var currentlocation = "tee";
break;
//etc
default :
var currentlocation = "green";
break;
}
The variable should be changed to the button's path accordingly, if it's correct that is.

Scottiedazzle
07-26-2005, 10:55 PM
I am working on the Switch function right now. Thanks for the input. Let me know when you have waded through the code.

Ricod
07-28-2005, 12:51 AM
Sorry, I still have problems seeing what's going on. If I saw an example file, I might be of more service.

Scottiedazzle
07-28-2005, 03:09 AM
It looks like the solution for me rested with this equation...

interactivebtnA = function () {
#include "holelocationvariable.as" //defines variables "currenthole" and "currentlocation"
var myButton = _root["hole" + currenthole + currentlocation + "1_btn"];

myButton.onRollOver = function (){
//do something

}

myButton.onRollOut = function () {
//do something
}
}

I had to equate the variable myButton to the string I was creating. But, the key thing that I was missing (after searching literally 100 posts on this site) was the fact that the string "hole" + currenthole + currentlocation + "1_btn" did not tell Flash that I was talking about a button instance. However, _root["hole" + currenthole + currentlocation + "1_btn"} told Flash that I was talking about a button instance. I am still learning Flash, so that terminology was foreign to me.

Thanks to Ricod for helping lead me to this answer.

Ricod
07-28-2005, 04:09 AM
Good for you !

Actually, the problem wasn't that 'my_Button' wasn't declared as a button (the onRollover etc. methods made it a button), but the path to it wasn't correct. Scope and such, I'm dealing with that problem atm, so I know what you mean with terminology problems. :rolleyes: