Hi everyone,
I am doing an AS3 application. In the begining of my AS file, I need to embeed different textures as the following:
Code:
[Embed (source = "./myImage1")]
public var Image1:Class;
I have to embeed n bitmaps.
Then I have to create an instance of each oh thease classes.
I want to store the names of thease classes in an array so I will be able to create an instance of a class by accessing the array.
Something like this:
Code:
var arrayOfClasses : Array = new Array();
arrayOfClasses.push(Image1);
arrayOfClasses.push(Image2);
...
Then I want to go through the array and do :
Code:
var variable : Object = new arrayOfClasses[0];
But this dosen't work !
How Can I build an array of classes ? Is it doable ??
I had the same question that moon21 had... and the cansers answer helped me tremendously... but I need to take it from there... please read the code below which is exact code from my movie which is a photo gallery, and try to remember... I am a noob so be gentle
var xmlurl = new URLLoader(new URLRequest("Gallery.xml"));
xmlurl.addEventListener(Event.COMPLETE, initGallery);
function initGallery(event:Event):void {
var spacing:Number = 65;
var xmlGallery:XML = new XML(xmlurl.data);
var imageCurrentNum:Number = 0;
var imageTotalNum:Number = xmlGallery.child("*").length();
var image:XMLList = xmlGallery.child("*");
var pic:XMLList = xmlGallery.child("*").attribute("pic");
var thumb:XMLList = xmlGallery.child("*").attribute("thumb");
var description:XMLList = xmlGallery.child("*").attribute("captions");
for (i=0; i<imageTotalNum; i++) {
//this is canser's code that returns a (.name = "image" + i)
var imageLoader:Loader = new Loader();
var thumbArray:Array = new Array();
var thumbLoader:Loader = new Loader();
thumbLoader.name = "image"+i;
thumbArray.push(thumbLoader);
thumbLoader.x = 0;
thumbLoader.y = spacing * i;
thumbsContainer.addChild(thumbLoader);
thumbLoader.load(new URLRequest (thumb[i]));
function thumbClick(evt:MouseEvent) {
imageLoader.load(new URLRequest (xmlGallery.child(evt.target.name).attribute("pic" )));
desc_txt.text = xmlGallery.child(evt.target.name).attribute("capti ons");
////this is where I get stumped...
////I need to turn the (.name = "image" + i) into a :Number type to update my imageCurrentNum:Number variable to update like the updateImage function does
//current_pos: = imageCurrentNum + 1;
//pos_txt.text = current_pos+" / "+imageTotalNum;
//trace(thumbArray[0]);
//trace(evt.target.name);
}
thumbLoader.addEventListener(MouseEvent.CLICK,thum bClick);
}
next_btn.addEventListener(MouseEvent.CLICK, nextImage);
previous_btn.addEventListener(MouseEvent.CLICK, prevImage);
imageLoader.addEventListener(Event.COMPLETE, updateImage);
function updateImage() {
current_pos = imageCurrentNum + 1;
pos_txt.text = current_pos+" / "+imageTotalNum;
desc_txt.text = description[imageCurrentNum];
imageContainer.addChild(imageLoader);
imageLoader.load(new URLRequest (pic[imageCurrentNum]));
}
function nextImage() {
if (imageCurrentNum<(imageTotalNum-1)) {
imageCurrentNum++;
updateImage();
}
}
function prevImage() {
if (imageCurrentNum>0) {
imageCurrentNum--;
updateImage();
}
}
updateImage();
}
function initGallery(event:Event):void {
var spacing:Number = 65;
var xmlGallery:XML = new XML(xmlurl.data);
var imageCurrentNum:Number = 0;
var imageTotalNum:Number = xmlGallery.child("*").length();
var image:XMLList = xmlGallery.child("*");
var pic:XMLList = xmlGallery.child("*").attribute("pic");
var thumb:XMLList = xmlGallery.child("*").attribute("thumb");
var description:XMLList = xmlGallery.child("*").attribute("captions");
for (i=0; i<imageTotalNum; i++) {
var imageLoader:Loader = new Loader();
var thumbArray:Array = new Array();
var thumbLoader:Loader = new Loader();
thumbLoader.name = "image"+i;
thumbArray.push(thumbLoader);
thumbLoader.x = 0;
thumbLoader.y = spacing * i;
thumbsContainer.addChild(thumbLoader);
thumbLoader.load(new URLRequest (thumb[i]));
function thumbClick(evt:MouseEvent) {
imageLoader.load(new URLRequest (xmlGallery.child(evt.target.name).attribute("pic" )));
desc_txt.text = xmlGallery.child(evt.target.name).attribute("capti ons");
imageCurrentNum = xmlGallery.child(evt.target.name).childIndex();
current_pos = imageCurrentNum + 1;
pos_txt.text = current_pos+" / "+imageTotalNum;
}
thumbLoader.addEventListener(MouseEvent.CLICK,thum bClick);
}
next_btn.addEventListener(MouseEvent.CLICK, nextImage);
previous_btn.addEventListener(MouseEvent.CLICK, prevImage);
function updateImage() {
current_pos = imageCurrentNum + 1;
pos_txt.text = current_pos+" / "+imageTotalNum;
desc_txt.text = description[imageCurrentNum];
imageContainer.addChild(imageLoader);
imageLoader.load(new URLRequest (pic[imageCurrentNum]));
}
function nextImage() {
if (imageCurrentNum<(imageTotalNum-1)) {
imageCurrentNum++;
updateImage();
}
}
function prevImage() {
if (imageCurrentNum>0) {
imageCurrentNum--;
updateImage();
}
}
updateImage();
}
Last edited by clp3777; 06-26-2009 at 05:39 PM.
Reason: cleaned up the code a bit