A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Store the name of classes ina n array

  1. #1
    Junior Member
    Join Date
    May 2007
    Posts
    4

    Store the name of classes ina n array

    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 ??

    Thinks

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    This is how you can do it:
    PHP Code:
    var classArray:Array = new Array();
    for (var 
    i=1i<=2i++)
    {
        var 
    _Class:Object getDefinitionByName("Class"+i) as Class;
        var 
    a:MovieClip = new _Class();
        
    a.name "a"+i;
        
    classArray.push(_Class);
        
    a.100*i;
        
    a.100*i;
        
    addChild (a);
    }
    trace(getChildByName("a1"));
    trace(classArray[0]); 
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Dec 2004
    Location
    KCMO
    Posts
    3

    THANK YOU!!! now more help please ;)

    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);

    thumbsContainer.mask = thumbsMask;
    imageContainer.mask = imageMask;

    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();
    }

  4. #4
    Junior Member
    Join Date
    Dec 2004
    Location
    KCMO
    Posts
    3
    oh... and here is the structure of the xml that gets parsed...

    <images>
    <image0 pic="images/1Rodeo.jpg" thumb="images/thumbs/1Rodeo.gif" captions="Rodeo 1"/>
    <image1 pic="images/2Rodeo.jpg" thumb="images/thumbs/2Rodeo.gif" captions="Rodeo 2"/>
    </images>

  5. #5
    Junior Member
    Join Date
    Dec 2004
    Location
    KCMO
    Posts
    3

    i just got it...

    var xmlurl = new URLLoader(new URLRequest("Gallery.xml"));
    xmlurl.addEventListener(Event.COMPLETE, initGallery);

    thumbsContainer.mask = thumbsMask;
    imageContainer.mask = imageMask;

    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

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    source file

    I have fixed some issues for the above code. I am attaching the source file for better understanding.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center