Skip to main content
Participating Frequently
August 19, 2010
Question

Trouble passing xml file name to function

  • August 19, 2010
  • 2 replies
  • 1253 views
Hi folks,

I'm new to AS3, I'm wondering if anyone could please help me out.

I have an index.swf that loads thumbnail images from this file (projects_list.xml).

When the user clicks on a thumbnail image I want it to pass the selected thumbnails "link" node information to the callFull function. You can see that I have managed to get it to work when I use the real path name. I just can't figure out how to pass the link node text.

Many Thanks
Sylvia

projects_list.xml file set up
<project id="0" name="Con" thumb="portfolio/images/con/thumbs/web_1_200pix.jpg" htm="" link="portfolio/xml/desc/desc_con.xml"></project>

desc_con.xml file set up (details file)
<project id="0" name="con">
<desc><![CDATA[<h2>Project: Con</h2><br/><p>Description of project here</p>]]></desc>
<full_images>
<logo tag="Logo">portfolio/images/con/full_size/logo_300pix.jpg</logo>
<web_1 tag="Web Page 1">portfolio/images/con/full_size/web_1_300pix.jpg</web_1>
<web_2 tag="Web Page 2">portfolio/images/con/full_size/web_2_300pix.jpg</web_2>
</full_images>
</project><br>

Here is the edited down version of my AS 3 code with just the snippets I thought were important.
AS CODE
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("portfolio/xml/projects_list.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
var myXML:XML = new XML(e.target.data);
my_project = myXML..project;
my_thumb_images = my_project.@thumb;
my_full_link= my_project.@link;
createContainer();
callThumbs();
}

function createContainer():void {
container_mc = new MovieClip();
container_mc.x = my_x;
container_mc.y = my_y;
addChild(container_mc);
container_mc.addEventListener(MouseEvent.CLICK, callFull);
}

function callThumbs():void {
for (var i:Number = 0; i < my_project_total; i++) {
var thumb_url= my_thumb_images;
var thumb_loader = new Loader();
thumb_loader.load(new URLRequest(thumb_url));
thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
thumb_loader.name = i;
var thumbText:String = my_thumb_name.toString();
htmlThumbText= thumbText;
thumbName.htmlText = htmlThumbText;
container_mc.addChild(thumbName);
}

function thumbLoaded(e:Event):void {
//trace("thumb loaded");
var my_thumb:Loader = Loader(e.target.loader);
container_mc.addChild(my_thumb);
}

function callFull(e:Event):void {
//var Full_xml= "portfolio/xml/desc/desc_con.xml";//this works
var Full_xml= my_full_link;//this works but it's loading all the xml files not just the one associated with the thumbnail I have selected
trace("Full_xml= "+Full_xml);
var myXMLURL:URLRequest=new URLRequest(Full_xml);
var urlLoader=new URLLoader(myXMLURL);
urlLoader.load(new URLRequest(Full_xml));
urlLoader.addEventListener(Event.COMPLETE,parseDescXML);
}

function parseDescXML(e:Event):void {
var myDescXML:XML = new XML(e.target.name);
my_full_link = myDescXML.project;
}
This topic has been closed for replies.

2 replies

SylviaMccAuthor
Participating Frequently
August 19, 2010


Hopefully this is better,

I'm new to AS3, I'm wondering if anyone could please help me out.

I have an index.swf that loads thumbnail images from this file (projects_list.xml).

When the user clicks on a thumbnail image I want it to pass the selected thumbnails "link" node information to the callFull function. You can see that I have managed to get it to work when I use the real path name. I just can't figure out how to pass the link node text.

Many Thanks
Sylvia

projects_list.xml file set up
<project id="0" name="Con" thumb="portfolio/images/con/thumbs/web_1_200pix.jpg" htm=""link="portfolio/xml/desc/desc_con.xml"></project>

desc_con.xml file set up (details file)
<project id="0" name="con">
<desc><![CDATA[<h2>Project: Con</h2><br/><p>Description of project here</p>]]></desc>
<full_images>
<logo tag="Logo">portfolio/images/con/full_size/logo_300pix.jpg</logo>
<web_1 tag="Web Page 1">portfolio/images/con/full_size/web_1_300pix.jpg</web_1>
<web_2 tag="Web Page 2">portfolio/images/con/full_size/web_2_300pix.jpg</web_2>
</full_images>
</project>

Below is the edited down version of my AS 3 code with just the snippets I thought were important.
AS CODE
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("portfolio/xml/projects_list.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
var myXML:XML = new XML(e.target.data);
my_project = myXML..project;
my_thumb_images = my_project.@thumb;
my_full_link= my_project.@link;
createContainer();
callThumbs();
}

function createContainer():void {
container_mc = new MovieClip();
container_mc.x = my_x;
container_mc.y = my_y;
addChild(container_mc);
container_mc.addEventListener(MouseEvent.CLICK, callFull);
}

function callThumbs():void {
for (var i:Number = 0; i < my_project_total; i++) {
var thumb_url= my_thumb_images;
var thumb_loader = new Loader();
thumb_loader.load(new URLRequest(thumb_url));
thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
thumb_loader.name = i;
var thumbText:String = my_thumb_name.toString();
htmlThumbText= thumbText;
thumbName.htmlText = htmlThumbText;
container_mc.addChild(thumbName);
}

function thumbLoaded(e:Event):void {
//trace("thumb loaded");
var my_thumb:Loader = Loader(e.target.loader);
container_mc.addChild(my_thumb);
}

function callFull(e:Event):void {
//var Full_xml= "portfolio/xml/desc/desc_con.xml";//this works
var Full_xml= my_full_link;//this works but it's loading all the xml files not just the one associated with the thumbnail I have selected
trace("Full_xml= "+Full_xml);
var myXMLURL:URLRequest=new URLRequest(Full_xml);
var urlLoader=new URLLoader(myXMLURL);
urlLoader.load(new URLRequest(Full_xml));
urlLoader.addEventListener(Event.COMPLETE,parseDescXML);
}

function parseDescXML(e:Event):void {
var myDescXML:XML = new XML(e.target.name);
my_full_link = myDescXML.project;
}
Inspiring
August 19, 2010

Your code has a lot of inefficiencies. Code below is based on my understanding of what you are trying to accomplish. I did not check it out - this is just a suggestion. Read comments.

Please pay attention in how I changed sequence of adding listeners to loaders vs. calling load() method - it is important!

In the future please format your code so that we don't have to do this job:

import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.EventDispatcher;
import flash.net.URLLoader;

var container_mc:MovieClip;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
myXMLLoader.load(new URLRequest("portfolio/xml/projects_list.xml"));

function processXML(e:Event):void {
     var myXML:XML = new XML(e.target.data);
     my_project = myXML..project;
     my_thumb_images = my_project.@thumb;
     my_full_link = my_project.@link;
     createContainer();
     callThumbs();
}

function createContainer():void {
     container_mc = new MovieClip();
     container_mc.x = my_x;
     container_mc.y = my_y;
     addChild(container_mc);
     // I guess the following line is not how you want to listen to thumbnails
     // feels like you need to add it to each thumbnail
     // see thumbLoaded function
     //container_mc.addEventListener(MouseEvent.CLICK, callFull);
}

function callThumbs():void {
     var thumb_loader:Loader;
     var thumbText:String;
     for (var i:int = 0; i < my_project_total; i++) {
          thumb_loader = new Loader();
          // names shouldn't be numbers
          thumb_loader.name = "thumb_" + i;
          thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
          thumb_loader.load(new URLRequest(my_thumb_images));
          thumbText = my_thumb_name.toString();
          htmlThumbText = thumbText;
          // what is thumbName?
          thumbName.htmlText = my_thumb_name.toString();
          container_mc.addChild(thumbName);
     }
}

function thumbLoaded(e:Event):void {
     //trace("thumb loaded");
     EventDispatcher(e.target).removeEventListener(Event.COMPLETE, thumbLoaded);
     var my_thumb:Loader = Loader(e.target.loader);
     my_thumb.addEventListener(MouseEvent.CLICK, callFull);
     container_mc.addChild(my_thumb);
}


function callFull(e:Event):void {
     // this is just a wild guess for I don't know your XML structure.
     var xmlPath:String = my_thumb_images[int(String(e.target.name).split("_")[1])];
     trace("xmlPath=", xmlPath);
     var urlLoader:URLLoader = new URLLoader();
     urlLoader.addEventListener(Event.COMPLETE, parseDescXML);
     urlLoader.load(new URLRequest(xmlPath));
}


function parseDescXML(e:Event):void {
     var myDescXML:XML = new XML(e.target.name);
     my_full_link = myDescXML.project;
}

SylviaMccAuthor
Participating Frequently
August 19, 2010

Thanks very much Andrea,

I tried your code (very clean!), unfortunately it didn't work and I didn't get any error messages.  I will have to take a closer look at your changes.

In my original post I didn't put a lot of the code as I thought it would be to confusing, it's a pretty complicated project.

Just curious I've rarely posted on the forums,  what do you mean when you ask me to "format my code"?

Very interesting how you coded the thumbnail function. Below is the complete code that I am using in my project. Would you still suggest that I make your changes to it?

function callThumbs():void {      for (var i:Number = 0; i < my_project_total; i++) {      var thumb_url= my_thumb_images;      //trace("thumb_url= "+thumb_url);      //     trace("");      var thumb_loader = new Loader();                thumb_loader.load(new URLRequest(thumb_url));                thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);      thumb_loader.name = i;      thumb_loader.x = (my_thumb_width+30)*x_counter;      thumb_loader.y = (my_thumb_height+15)*y_counter;                 var thumbText:String = my_thumb_name.toString();                //trace("thumbText= "+thumbText);            htmlThumbText= thumbText;      //trace("thumb_url= "+thumb_url);      //thumb name      var thumbName:TextField = new TextField();      //butText.embedFonts = true;      thumbName.autoSize = TextFieldAutoSize.LEFT;      //butText.antiAliasType = AntiAliasType.ADVANCED;      thumbName.defaultTextFormat = format;      thumbName.selectable = true;      thumbName.multiline = false;      thumbName.wordWrap = true;      thumbName.width=200;      //thumbName.x = thumb_loader.x - (thumb_loader.width/2);      thumbName.x = thumb_loader.x;           thumbName.y = 180;      thumbName.htmlText = htmlThumbText;      container_mc.addChild(thumbName);           if (x_counter+1 < columns) {                x_counter++;           } else {                x_counter = 0;                y_counter++;           }      //preloader for thumbnail images      var preloader_pb:ProgressBar = new ProgressBar();      preloader_pb.source = thumb_loader.contentLoaderInfo;      preloader_pb.x = thumb_loader.x;      preloader_pb.y = thumb_loader.y;      preloader_pb.width = my_thumb_width;      preloader_pb.height = my_thumb_height;      preloaders_mc.addChild(preloader_pb);      preloader_pb.addEventListener(Event.COMPLETE, donePb);      } }

This is also very interesting. Could you explain what is happening in this line for me?

var xmlPath:String = my_thumb_images[int(String(e.target.name).split("_")[1])];

Many Thanks for your help,

Sylvia

Inspiring
August 19, 2010

Your code is difficult to read. You will have a better chance to have people look into it if you format the code (indent) and use code highlight feature (arrow next to the smiley --> syntax highlighting --> Java).