Skip to main content
Inspiring
December 16, 2008
Answered

Help applying an action to a bunch of child movie clips

  • December 16, 2008
  • 5 replies
  • 464 views
Hi folks,

I'm trying to build a dealer locator map. I figured it would take me a day to figure out, and I've been struggling with it for a week. I've got almost all of it done, but there's one thing I still can't get a handle on.

I've got an accordion component containing the list of dealers called "myAccordion". I'm loading an xml file in it like so:

var my_xml:XML = new XML();
my_xml.ignoreWhite = true;
my_xml.onLoad = function(success:Boolean) {
myLabel.text = this.firstChild.childNodes[0];
myTextArea.text = this.firstChild.childNodes[1];
myTextArea2.text = this.firstChild.childNodes[2];
myTextArea3.text = this.firstChild.childNodes[3];
myTextArea4.text = this.firstChild.childNodes[4];
};
my_xml.load("ny.xml");

Then I have a movieclip called "map". In it are 50 child movie clips (one for each state) which are named "NY" (for new york), "WA" (for washington), etc.

When someone clicks on a state, I need 2 things to happen:

1. change the color of the state
3. load an xml doc with the same name (wa.xml, ny.xml, etc.) into myAccordion

How do I apply an action to all of the children of the map that will do that?
This topic has been closed for replies.
Correct answer tactics
A coworker of mine figured it out using something like this:

// function to set color to active state
var setActive:Function = function() {
this.myColor = new Color(this) ;
this.myColor.setRGB(0xE1AD3F) ;

}

//function to set color to inactive state
var setInactive:Function = function() {
this.myColor = new Color(this) ;
this.myColor.setRGB(0xEBE9ED) ;
}

// function to handle on press event.
var myOnPress:Function = function() {
// loop through everything on _root.map, and deactivate.
for (i in _root.map) {
if (typeof(_root.map ) == "object") {
_root.map
.setInactive();
}
}
// set myself active
this.setActive() ;

// determine our xml to load, based on my name.
var n = this._name.substr(2, this._name.length) + ".xml" ;
trace("press function called on: " + this._name) ;
_root.my_xml(n);
}

// this would be your xml object,
var my_xml = function(n) {
trace("myxml called: " + n) ;
}

// loop through everything in the _root.map movie clip, and assign functions.
for (i in _root.map) {
if (typeof(_root.map ) == "object") {
_root.map
.setActive = setActive ;
_root.map .setInactive = setInactive ;
_root.map
.onPress = myOnPress ;
// initialize our inactive state
_root.map .setInactive();
}
}

Thanks for the help.

5 replies

kglad
Community Expert
Community Expert
December 16, 2008
you're welcome.
tacticsAuthorCorrect answer
Inspiring
December 16, 2008
A coworker of mine figured it out using something like this:

// function to set color to active state
var setActive:Function = function() {
this.myColor = new Color(this) ;
this.myColor.setRGB(0xE1AD3F) ;

}

//function to set color to inactive state
var setInactive:Function = function() {
this.myColor = new Color(this) ;
this.myColor.setRGB(0xEBE9ED) ;
}

// function to handle on press event.
var myOnPress:Function = function() {
// loop through everything on _root.map, and deactivate.
for (i in _root.map) {
if (typeof(_root.map ) == "object") {
_root.map
.setInactive();
}
}
// set myself active
this.setActive() ;

// determine our xml to load, based on my name.
var n = this._name.substr(2, this._name.length) + ".xml" ;
trace("press function called on: " + this._name) ;
_root.my_xml(n);
}

// this would be your xml object,
var my_xml = function(n) {
trace("myxml called: " + n) ;
}

// loop through everything in the _root.map movie clip, and assign functions.
for (i in _root.map) {
if (typeof(_root.map ) == "object") {
_root.map
.setActive = setActive ;
_root.map .setInactive = setInactive ;
_root.map
.onPress = myOnPress ;
// initialize our inactive state
_root.map .setInactive();
}
}

Thanks for the help.
tacticsAuthor
Inspiring
December 16, 2008
But if I do that, won't I still have to write 50 different instances of that script (one for each state)?

Like:

map.OR_b.onPress=function(){
my_xml.load(this._name.substr(0,2)+".xml");
this.col.setRGB(0xE1AD3F);
}

map.TX_b.onPress=function(){
my_xml.load(this._name.substr(0,2)+".xml");
this.col.setRGB(0xE1AD3F);
}

etc.
tacticsAuthor
Inspiring
December 16, 2008
Thanks. I figured out how to do the setRGB thing on a per instance basis, and did a little reading on the "_name" property.

I changed the name of the buttons to "b_WA", etc. and then wrote this:

myColor = new Color("map.b_name");

map.b_name.onPress=function(){
my_xml.load("_name.xml");
myColor.setRGB(0xE1AD3F);
}

But obviously, that's not quite right. Any suggestions?
tacticsAuthor
Inspiring
December 16, 2008
OK, obviously "b_WA" won't work as a naming convention, so I switched them to "WA_b", "OR_b", etc. but I'm still having issues. I think part of it needs to be:

if(map.indexOf("_b")
kglad
Community Expert
Community Expert
December 16, 2008
try:

kglad
Community Expert
Community Expert
December 16, 2008
1. create a color instance for each state (eg, wa.col=new Color(wa) ) and use setRGB(wa.col) to change the color of your states.

2. use the _name property of your child movieclips to obtain the xml name and load it.