Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load XML images into FLASH as an Array

Status
Not open for further replies.

ShonGill

Programmer
May 20, 2008
44
US
I have the below ActionScript that works if I remove the loadXML and use an array that I define the image names in. I'm trying to load the image names from the XML file.

I think the problem is in parsing the XML. I can trace my photoXML anywhere in the script and it will output the entire contents of my XML file, however for some reason I cannot seem to get the file name out of the XML file into the array. So, the thumbnails are not loading into the movieclip. I'm I missing something?

XML File:
Code:
<root>
  <image>
    <IMG_ID>1</IMG_ID>
    <file>FileName0.jpg</file>
    <name>Micros Boarding</name>
    <brand>Micros</brand>
    <description>Just a quick description of file</desc>
    <img>image0.jpg</img>
    <thumb>image0_thumb.jpg</thumb>
  </image>
  <image>
    <IMG_ID>2</IMG_ID>
    <file>FileName1.jpg</file>
    <name>Indigo Star - Wave Surfing</name>
    <brand>Indigo Star</brand>
    <desc>Dude surfing on a huge wave!</desc>
    <img>image1.jpg</img>
    <thumb>image1_thumb.jpg</thumb>
  </image>
  <image>
    <IMG_ID>3</IMG_ID>
    <file>FileName2.jpg</file>
    <name>Micros More Boarding</name>
    <brand>Micros</brand>
    <desc>Skateboarding in empty swimming pool.</desc>
    <img>image2.jpg</img>
    <thumb>image2_thumb.jpg</thumb>
  </image>
</root]

Actionscript:
Code:
var photoXML:XML = new XML();
photoXML.ignoreWhite = true;
photoXML.load("MY XML LOCATION");
photoXML.onLoad = loadXML;

currentHolder = 0; 
theDepth = 1000; 
_global.imageLoaded = false;
_global.selectedImage = 0;

this.createEmptyMovieClip("img0_mc", theDepth++ + 10000); 
this.createEmptyMovieClip("img1_mc", theDepth++ + 10000);

function loadXML (loaded) {
	if (loaded) {
		var xmlParse:XmlNode = photoXML.firstChild;
		var thumb_arr:Array = new Array();
		total = xmlParse.childNodes.length;
		for (i = 0; i < total; i++) {
			thumb_arr[i] = xmlParse.childNodes[i].childNodes[6].firstChild.nodeValue;
			this.createEmptyMovieClip("thumb" + i + "_mc", theDepth++);
			this.createEmptyMovieClip("thumb" + i + "_tmp_mc", theDepth++);	
			this["thumb" + i + "_mc"]._x = 6;	
			this["thumb" + i + "_mc"]._y = 10 + 108 * i;	
			this["thumb" + i + "_mc"].loadMovie("IMAGE FOLDER LOCATION" + thumb_arr[i]);
			
			this["thumb" + i + "_tmp_mc"].no = i;	
			this["thumb" + i + "_tmp_mc"].onEnterFrame = function()	{
				var l = this._parent["thumb" + this.no + "_mc"].getBytesLoaded();
				var t = this._parent["thumb" + this.no + "_mc"].getBytesTotal();
				if ((l >= t) && (t > 1) && (this._parent["thumb" + this.no + "_mc"]._width > 1))  {
					this._parent["thumb" + this.no + "_mc"].num = this.no;
					this._parent["thumb" + this.no + "_mc"].onPress = function()  {
						if ((_global.selectedImage != this.num) && (_global.imageLoaded))  {
							loadImage(this.num);
						}
					};
					delete this.onEnterFrame;
					this.removeMovieClip();
				}
			};
			
		};
	}
};

Dishon Gillis
Dominion Enterprises Company
 
You're doing something wrong there ;)

This is how I'd do it - much simpler:
Code:
import mx.utils.Delegate;

var photoXML:XML = new XML();
photoXML.ignoreWhite = true;
photoXML.load("test.xml");
photoXML.onLoad = Delegate.create(this, loadXML);

function loadXML(loaded):Void {
   if (loaded) {
      var xmlParse:XMLNode = photoXML.firstChild;
      var total:Number = xmlParse.childNodes.length;
      for (var i:Number = 0; i < total; i++) {
         var thumbFile = xmlParse.childNodes[i].childNodes[6].firstChild.nodeValue;
				
         var thumbMC:MovieClip = this.createEmptyMovieClip("thumb" + i + "_mc", this.getNextHighestDepth());
         thumbMC["num"] = i;
         with(thumbMC){
            _x = 6;
            _y = 10 + 108*i;
         }
         thumbMC.onPress = function():Void {
            trace(this.num);
         }
				
         var thumbImg:MovieClip = thumbMC.createEmptyMovieClip("img", 1);
         thumbImg.loadMovie(thumbFile);
      }
   }
}
You should get the number on press.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top