0
votes

I'm using Flash AS4 and Ive created and array that holds xml data which works fine with the following loop

for(var i:int = 0; i < uNavXML.length(); i++){
    navArray.push(uNavXML..navRef.text()[i]);

The XML data is actually the names of movie clips that I have on stage:

    xmlLIST :<uNav>
  <unavNavID>1</unavNavID>
  <navRef>navBtns.nav01</navRef>
</uNav>
<uNav>
  <unavNavID>2</unavNavID>
  <navRef>navBtns.nav02</navRef>
</uNav>
<uNav>
  <unavNavID>3</unavNavID>
  <navRef>navBtns.nav03</navRef>
</uNav>
<uNav>
  <unavNavID>4</unavNavID>
  <navRef>navBtns.nav04</navRef>
</uNav>
<uNav>
  <unavNavID>5</unavNavID>
  <navRef>navBtns.nav05</navRef>
</uNav>
<uNav>
  <unavNavID>7</unavNavID>
  <navRef>navBtns.nav07</navRef>
</uNav>
<uNav>
  <unavNavID>8</unavNavID>
  <navRef>navBtns.nav08</navRef>
</uNav>
<uNav>
  <unavNavID>10</unavNavID>
  <navRef>navBtns.nav10</navRef>
</uNav>
<uNav>
  <unavNavID>12</unavNavID>
  <navRef>navBtns.nav12</navRef>
</uNav>
<uNav>
  <unavNavID>13</unavNavID>
  <navRef>navBtns.nav13</navRef>
</uNav>
<uNav>
  <unavNavID>14</unavNavID>
  <navRef>navBtns.nav14</navRef>
</uNav>
<uNav>
  <unavNavID>15</unavNavID>
  <navRef>navBtns.nav15</navRef>
</uNav>

So, what I want to do is to take these xml values, make them objects in the for loop so that I can then assign eventlisteners to them like this

navObj[i].addEventListener(MouseEvent.MOUSE_OVER, mOvNav); navObj[i].addEventListener(MouseEvent.MOUSE_OUT,mOuNav); navObj[i].addEventListener(MouseEvent.CLICK,goPage);

Is there a way to do this in Flash actionScript? I'm not terribly experienced with AS3 and I've never had to convert an array from String to Object before so I'm struggling. If anyone can off help it would be warmly received...

Answer found via this and on previous SO quesiotn Converting String values to MovieClip in AS 3.0

For my issue here was the answer to remove the "navBtns." prfix and just supply the navButton instance name via navShort

for each (var nav:String in navShrt) {
    var navInst:MovieClip = navBtns[nav] as MovieClip;

Then I was able to reference via navInst.addEventListener.... etc

1

1 Answers

0
votes

Lets say you have a MovieClip called "testMC" and one called "test2MC". And in your XML you have stored those names in some kind of fashion.

You iterate though the XML and check the contents of it, every piece of content holds the name of one of your MovieClips.

Then you simply have to check if the contents of the XML match up to your criteria.

Something along the lines of this:

for(var i:int=0;i<uNavXML.length();i++){
    var mcname:String = uNavXML..navRef.text()[i];
    switch(mcname){
        case "testMC":
            //do whatever you need with your testMC Object
        break;
        case "test2MC":
            //do whatever you need with your test2MC Object
        break;
    }
}

Now, I don't quite understand what you mean by "convert xml text values into objects". If your intend is to create new MovieClips of a specific type, defined in your XML. My suggested approach will work, but you need to have those MovieClips in your Library and give them all AS Linkage.

For example, you could have a MovieClip called "testMC" in your library, and it has the AS Linkage Class called "Test".

In the above code, you could do the following then:

case "testMC":
    var newMC:Test = new Test();
    addChild(newMC);
break;

Thus, you can spawn multiple instances of an Object of your choosing, defined in the XML.


Alright, second attempt now that I understand your question better.

  • On your stage, you have a MovieClip called navBtns that holds the named instances of your Button MovieClips.

  • In your XML, you are referring to these buttons through their parent, like navBtns.nav01

This means that every node in your XML is essentially referring to 2 Objects, not one. One for navBtns, and one for the actual button.

I propose this solution, albeit there should be better ones:

  • Iterate through the XML

  • Split the string contained in a XML node, split it at "."

  • directly modify the defined instances through bracket notation

Which would look like this:

for(var i:int=0;i<uNavXML.length();i++){
    //obtain the entire string
    var nodeTxt:String = uNavXML..navRef.text()[i];
    //split the string at the dot
    var split:Array = nodeTxt.split(".");
    //not necessary, just for better understanding, create two helper  variables
    var prnt:String = split[0];
    var btn:String = split[1];
    //obtain MovieClip instance through bracket notation
    var obj:MovieClip = this[prnt][btn];
    //do what you need with the MovieClip
}

Keep in mind, this is untested.