Marianna
Marianna

Reputation: 129

how to add each element of an array to the stage as3

I have an array with movieclips (r1, r2, etc) and tThose movieclips must been hidden until there is a hitestobject so i have this code at first

var replace:Array = [r1,r2,r3,r4,r5,r6,r7,r8]
var len:int = replace.length;
for( var o:int = 0; o < len; o++ )
    this.removeChild( replace[o] );
  replace.length = 0;

I want to add each element of that array on stage every time i have a hittestoblect. Something is wrong with my code because only r1 is added on stage and i don't know how to fix it. Can you please help me?

 function releaseToDrop(e:MouseEvent):void
 {
     Star(e.target).stopDrag();
     if (Star(e.target).hitTestObject(target))
   {
      removeChild(Star(e.target));
      for(var o:uint = 0;o<7;o++){
      var replace:Array = [r1,r2,r3,r4,r5,r6,r7,r8]
            addChild(replace[0])

 } 

}

Upvotes: 0

Views: 105

Answers (1)

Pete TNT
Pete TNT

Reputation: 8403

This line:

addChild(replace[0]);

Always adds the first child, to add a specific object, you replace 0 with whatever index you have. If you want to add them one by one, as in one after another every time there's a hit, you should have counter outside of the function that gets incremented every time there's a hit

var counter:int = 0;

function releaseToDrop(e:MouseEvent):void
{
   Star(e.target).stopDrag();
   if (Star(e.target).hitTestObject(target))
   {
     removeChild(Star(e.target));
     var replace:Array = [r1,r2,r3,r4,r5,r6,r7,r8]
     addChild(replace[counter]);
     counter = counter+1;
   } 
 }

Upvotes: 1

Related Questions