A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Duplicating Children with For Loop But Need each object to have a Unique Name

Threaded View

  1. #1
    Senior Member
    Join Date
    Feb 2006
    Location
    Portland OR
    Posts
    138

    Duplicating Children with For Loop But Need each object to have a Unique Name

    I'm duplicating a sprite with this for loop and placing them sequentially on the stage. However they all seem to have the same name.

    Any help on how to get them to be have unique "instance" names so I can access them individually or populate them with unique content?



    public function galleryItem():void{

    for (var i:uint=1; i<=5; i++) {
    thumbBox.name = "thumbBox"+i;
    thumbBox.graphics.beginFill(gBgColor, 1);
    thumbBox.graphics.drawRect((gPosX+gWidth)*i, gPosY, gWidth, gHeight);
    thumbBox.graphics.endFill();
    thumbBox.alpha = .5;
    thumbBox.buttonMode = true;
    trace("ThumBoxNum: "+thumbBox.name);
    addChild(thumbBox);
    }

    thumbBox.addEventListener(MouseEvent.CLICK,countIt );
    }

    public function countIt(event:MouseEvent):void{
    trace("Clicked: "+thumbBox.name);
    }
    }


    So, when I trace before the addChild I get: ThumBoxNum: thumbBox1 | thumbBox2 | etc...
    when I trace it in the countIt function I get: Clicked: thumbBox5 for each object on the stage.

    And if this is possible it would be ideal to set up a single event listener for each:
    thumbBox[i].addEventListener(MouseEvent.CLICK,countIt);
    Obviously I'm new to AS3 and trying to make the switch, have mercy .
    thanks
    Last edited by knottyDJ; 10-15-2008 at 08:15 PM.

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