PDA

View Full Version : very n00b question



tuga74
March 6th, 2008, 01:49 PM
Hi there this forum has become my number 1 online resource for learning as3, great work guys.

Now my stupid question:

I'm starting in AS3 so i'm testing a couple of things, i would like to define some properties before i create a class. What i'm trying to achieve is to set a property of a object and then run that object.

This is my code inside my as3 file:

package com.actual.slideshow
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.MorphShape;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.net.*;
import flash.events.Event;

public class Slideshow extends Sprite
{
public var pos:int = 0;
private var _xmlFile:String;



public function Slideshow():void
{

loadXML(_xmlFile);
}



public function loadXML(fileName:String):void {
trace(fileName);
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, completeLoadXML);
ldr.load(new URLRequest(fileName));

}


public function get xmlFile():String {
return this._xmlFile;

}

public function set xmlFile(str:String):void {
this._xmlFile = xmlFile;

}
private function completeLoadXML(e:Event):void {
var xml:XML = new XML(e.target.data);
var xmlList:XMLList = xml.photo;
for (var i:int = 0; i < xmlList.length(); i++) {
var ldr:Loader = new Loader();
ldr.load(new URLRequest("\images\\"+xml.photo.text() [i]));
ldr.contentLoaderInfo.addEventListener(Event.COMPL ETE, onLoadImage);
}


}

private function onLoadImage(e:Event):void {
var bmpOriginal:Bitmap = Bitmap(e.target.content);
var bmp:Bitmap = new Bitmap(bmpOriginal.bitmapData.clone());
bmp.width = 50;
bmp.height = 50;
bmp.x = pos;
addChild(bmp);
pos += bmp.width;

}
}

And then on Flash i could use something like:


var sl:Slideshow = new Slideshow();
sl.xmlFile = "imglist.xml";As i would like to pass a lot of variables as the xmlFile, position, width, etc., i would not like to write var sl:Slideshow = new Slideshow(xmlFile);

Please can someone help me or point to some tutorials on this. Btw is this the right approach.

Thanks

ActionScript3
March 6th, 2008, 03:47 PM
how about make your class as dynamic?
public dynamic class Slideshow extends Sprite
{...
and you can
sl.position = ...
sl.other = ...

McGuffin
March 6th, 2008, 03:55 PM
^ That's not his issue, as far as I can tell.



var sl:Slideshow = new Slideshow();
sl.xmlFile = "imglist.xml";
sl.pos = 0;
sl.build();


where build is a function that simply does your loadXML(_xmlFile) call instead of having that in the constructor. There's no way to have a constructor wait to execute until you set variables externally and then run, nor is there a reason why you couldn't just set up another function like above :)