开发者

simply load image1 and image2 from xml and place in respective MC's on stage

开发者 https://www.devze.com 2023-02-27 08:32 出处:网络
this is drivi开发者_如何学编程ng me nuts. What I want should be super simple but they seem to have made it dificult and people on forums seem intent on giving complicated answers. please help with a s

this is drivi开发者_如何学编程ng me nuts. What I want should be super simple but they seem to have made it dificult and people on forums seem intent on giving complicated answers. please help with a simple solution.

I have an XML with lots of data in called userData.xml that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<videos>
    <vid src="videos/video1.flv"/>
    <vid src="videos/video2.flv"/>
</videos>
<images>
    <img src="images/image1.jpg"/>
    <img src="images/image2.jpg"/>
</images>

ALL I want to do is (using AS3) get image1.jpg and add it to imageHolderOne_mc and get image2.jpg add it to imageHolderTwo_mc. Completely ignoring the videos part of the XML (which must stay for other uses)

I realy hope someone can help Thanks in advance


have a look at the xml primer from republicofcode and how to load images dynamically in as3

basically something like...

var myXML:XML;

var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("yourxmlfile.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {
    myXML = new XML(e.target.data);
    for each(var imgSrc:String in myXML.images.*.@src){
        createLoader(imgSrc, #NAME_OF_MOVIE_CLIP#);
    }
}

function createLoader ($imgUrl:String, $placeholder:MovieClip){
    var myLoader:Loader = new Loader();
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);

    var fileRequest:URLRequest = new URLRequest($imgUrl);
    myLoader.load(fileRequest);

    function onLoaderReady(e:Event) {     
          // the image is now loaded, so let's add it to the display tree!     
        $placeholder.addChild(myLoader);
    }
}


Try wrapping in a root tag. Root tags get ignored with e4x standard/
So by not having a root tag the XML parser gets confused.
When accessing the data you will also ignore the root tag and not refer to it.

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <videos>
      <vid src="videos/video1.flv"/>
      <vid src="videos/video2.flv"/>
  </videos>
  <images>
      <img src="images/image1.jpg"/>
      <img src="images/image2.jpg"/>
  </images>
</root>

Accessed like so.

    var myXML:XML = new XML(
    <root>
      <videos>
          <vid src="videos/video1.flv"/>
          <vid src="videos/video2.flv"/>
      </videos>
      <images>
          <img src="images/image1.jpg"/>
          <img src="images/image2.jpg"/>
      </images>
    </root>
    );

trace(myXML)
    for each(var img:XML in myXML.images.img ){
      trace( img.@src )
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号