PDA

View Full Version : Display Captions from XML


cultivate
11-01-2009, 11:06 PM
Hi I'm new to action script and I'm working on making a little photo gallery... I've got the photos displaying from an xml file the way I'd like but I'm having trouble trying to get the photos caption to display.

Here is want I have so far. Right now only the last caption in the loop is displaying. I would like to have the caption only display when you click on the photo. I've sort of come to a stand still when it come to doing this and... any insight would be a big help. Thanks!

package {
import flash.display.Loader;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.events.MouseEvent;

import flash.events.TextEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;


public class PhotoGallery extends MovieClip {

public var thisimg:String;
public var picloader:Loader;
public var mycontent:XML;
public var thumb:MyPhoto;
public var myURL:URLRequest;
public var newloader:URLLoader;
private var thiscaption:String;
public var caption:TextField;


public function PhotoGallery():void {
myURL=new URLRequest("gallery.xml");
newloader=new URLLoader;
newloader.load(myURL);
newloader.addEventListener(Event.COMPLETE,dataLoad ed);
}

private function dataLoaded(evt:Event):void {

caption = new TextField();
caption.autoSize=TextFieldAutoSize.LEFT;
caption.multiline=true;
caption.x=1;
caption.y= 1;


var mycontent:XML=new XML(newloader.data);
for (var i:uint=0; i<mycontent.photo.length(); i++) {
thisimg=mycontent.photo[i].file;
thiscaption=mycontent.photo[i].caption;
caption.htmlText = "test caption"+mycontent.photo[i].caption+"endcaption";
addChild (caption);
picloader=new Loader//creates a new loader
picloader.load(new URLRequest(thisimg));//loads the information
thumb=new MyPhoto();
thumb.addEventListener(MouseEvent.ROLL_OVER,bringt oTop);
thumb.addChild(picloader);
addChild(thumb);//add thumb to the stage

}
}

private function bringtoTop(evt:MouseEvent):void {
setChildIndex(DisplayObject(evt.target), numChildren - 1);//recasting an object into a dsiplay object & then set a new layer for an object
}
}
}

elytexeira
11-02-2009, 12:09 AM
You need to create the Textfield instance inside the loop:

class PhotoGallery extends MovieClip {

public var thisimg:String;
public var picloader:Loader;
public var mycontent:XML;
public var thumb:MyPhoto;
public var myURL:URLRequest;
public var newloader:URLLoader;
private var thiscaption:String;
public var caption:TextField;


public function PhotoGallery():void {
myURL=new URLRequest("gallery.xml");
newloader=new URLLoader;
newloader.load(myURL);
newloader.addEventListener(Event.COMPLETE,dataLoad ed);
}

private function dataLoaded(evt:Event):void {

var caption:TextField

var mycontent:XML=new XML(newloader.data);
for (var i:uint=0; i<mycontent.photo.length(); i++) {
thisimg=mycontent.photo[i].file;
caption =new TextField();
caption.autoSize=TextFieldAutoSize.LEFT;
caption.multiline=true;
caption.x=1;
caption.y= 1;
thiscaption=mycontent.photo[i].caption;
caption.htmlText = "test caption"+mycontent.photo[i].caption+"endcaption";
addChild (caption);
picloader=new Loader//creates a new loader
picloader.load(new URLRequest(thisimg));//loads the information
thumb=new MyPhoto();
thumb.addEventListener(MouseEvent.ROLL_OVER,bringt oTop);
thumb.addChild(picloader);
addChild(thumb);//add thumb to the stage

}
}

private function bringtoTop(evt:MouseEvent):void {
setChildIndex(DisplayObject(evt.target), numChildren - 1);//recasting an object into a dsiplay object & then set a new layer for an object
}
}

Because what you were doing before was to change the properties of the same instance many times.


Currently Working at Probooks - libros (http://www.probooks.com.mx)

cultivate
11-02-2009, 01:06 AM
Thank you for the insight, but how would i go about nesting the captions so they appear on the stage with their image instead of just piled on top of each other.