PDA

View Full Version : Attaching button functions to an array of MCs


PhineasX
03-13-2006, 04:06 PM
The Scenario: I have a map of the US where each state is a movie clip. An array called "activeStates" controls which of the states will be clickable (with onRelease(), onRollOver(), etc...)

For example the array may be as follows:

var activeStates = ["CO", "WA", "NY", "CA", "IL"];


Now, I'm trying to write Actionscript that will step through this array, and assign the proper button functions to each individual state MC. What I came up with (which is not working) is:


for (var i = 0; i<activeStates.length; i++) {
btnState = activeStates[i];
this.btnState.onRollOver = function() {
trace("rollover");
};
this.btnState.onRelease = function() {
trace("click");
};
}


Obviously the ultimate goal is to have more function than trace actions, but I'm sure you can see where I'm trying to go here. If someone can explain to me the error of my ways on this, I would be much obliged.

Xeef
03-13-2006, 04:18 PM
//as the array contain STRINGS
//_root is the parent of the Clip
//eg there woud by a clip _root.CO, _root.WA ....
btnState = _root[activeStates[i]];
btnState.onRollOver = function() {
trace("rollover");
};
btnState.onRelease = function() {
trace("click");
};


you also can put streigth the instanceName in the array

//if we are on _root and the clips also
var activeStates = [CO, WA, NY, CA, IL];
//else way you need corect pathing
//eg.
//
var activeStates = [_root.MasterClip.BigCity.CO, _root.MasterClip.City.WA, _root.OtherClips.NY....];
...
..
activeStates[i].btnState.onRollOver = function() {
trace("rollover");
};
...
..

PhineasX
03-13-2006, 04:52 PM
I should have been clear. The script is on the same timeline as the MCs it is controlling.

I've changed my array to be direct references to the MC instance names instead of strings (not sure why I was doing it as strings to begin with), but it's still not working.

What I'm not getting is that I'm using a similar loop to change colors on the active states, like so:


var activeStates = [CO, WA, NY, CA, IL];
for (var i = 0; i<activeStates.length; i++) {
active = activeStates[i];
var stateColor = new Color(active);
stateColor.setRGB(0x990000);
}


And that works exactly as expected. So why doesn't this work the same?


for (var i = 0; i<activeStates.length; i++) {
btnState = [activeStates[i]];
this.btnState.onRollOver = function() {
trace("rollover");
};
this.btnState.onRelease = function() {
trace("click");
};
}


I should add, when I explicitly set these properties like this...


this.CO.onRollOver = function () {
trace("rollover");
}

this.CO.onRelease = function () {
trace("click");
}


They work just fine. So what the heck? Is there some other syntax/scope/path issue I'm not seeing?

Xeef
03-13-2006, 06:32 PM
btnState = [activeStates[i]];

is the same as :

btnState = new Array(activeStates[i]);


so let the [] away ! :p

PhineasX
03-13-2006, 07:01 PM
I realized that I was complicating things more than necessary. I substituted


this.activeStates[i].onRollOver = function() {
trace("rollover");
};


And it works just fine. Makes much more sense just using a direct reference. Don't know why it didn't occur to me to begin with.

But thanks, Xeef, for helping me think through it.