PDA

View Full Version : Problem with XML gallery



benny1989
December 5th, 2008, 10:03 AM
Hi,
I'm pretty new to as3, and i'm having problems converting a xml gallery that used timeline actionscript, to a xml gallery that's being controlled by a class file. Here is my code for the attempted class file version;



package {

import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import caurina.transitions.Tweener;

public class Gallery extends MovieClip {

var xmlLoader:XML;
var imageLoader:Loader;
var xml:XML;
var xmlList:XMLList;
public function Main() {
xmlLoader.load(new URLRequest("images.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}

function xmlLoaded(event:Event):void {

xml = XML(event.target.data);
xmlList = xml.children();

for (var i:int = 0; i < xmlList.length(); i++) {

imageLoader = new Loader();
imageLoader.load(new URLRequest(xmlList[i].attribute("thumb")));
imageLoader.x = 25;
imageLoader.y = i * 150 + 25;
imageLoader.name = xmlList[i].attribute("source");
addChild(imageLoader);
imageLoader.addEventListener(MouseEvent.MOUSE_OVER , showPicture);
trace("working");
}
}

function showPicture(event:MouseEvent):void {

imageLoader = new Loader();
imageLoader.load(new URLRequest(event.target.name));
imageLoader.alpha = 0;
imageLoader.x = 400;
addChild(imageLoader);
Tweener.addTween(imageLoader,{alpha:1,time:36,dela y:2,transition:"easeOutBack",useFrames:true});
}

}
}



But when I run the file, the following errors are displayed;

Warning: 3594: load is not a recognized method of the dynamic class XML.
Warning: 3594: addEventListener is not a recognized method of the dynamic class XML.

here is the code for the xml gallery using the as3 on the timeline;



import caurina.transitions.Tweener;

var imageLoader:Loader;
var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();

xmlLoader.load(new URLRequest("images.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void {
xml = XML(event.target.data);
xmlList = xml.children();
for (var i:int = 0; i < xmlList.length(); i++) {
imageLoader = new Loader();
imageLoader.load(new URLRequest(xmlList[i].attribute("thumb")));
imageLoader.x = 25;
imageLoader.y = i * 150 + 25;
imageLoader.name = xmlList[i].attribute("source");
addChild(imageLoader);
imageLoader.addEventListener(MouseEvent.MOUSE_OVER , showPicture);
trace("working");
}
}

function showPicture(event:MouseEvent):void {
imageLoader = new Loader();
imageLoader.load(new URLRequest(event.target.name));
imageLoader.alpha = 0;
imageLoader.x = 400;
addChild(imageLoader);
Tweener.addTween(imageLoader,{alpha:1,time:36,dela y:2,transition:"easeOutBack",useFrames:true});
}



Sorry if my explanation was vague.
Thanks
Benny

schnitzel
December 5th, 2008, 12:10 PM
compare the two code snippets, and you will see that you are missing something ;)

>> var xmlLoader:URLLoader = new URLLoader();

Sage_of_Fire
December 5th, 2008, 02:30 PM
This doesn't seem to be related to your problem, but you should name the constructor of a class the same as the class itself. So, your first function inside Gallery, the one that runs first, should be called Gallery, not Main. I'm not sure how the class is working at all right now, but even if the name is not required, it's good practice.

Also, schnitzel had it right about the URLLoader, but he forgot something. You should declare xmlLoader as URLLoader instead of XML. Otherwise, even if you follow his advice, you'll get an error.