views:

174

answers:

4

I cannot figure out what my object instance names are because of how I am generating them and adding them to the stage...

var circles:Array = new Array() //Declare array for displaying multiple objects
function spawn(evt:MouseEvent):void { //MouseEvent trigger, working
 for (var i:int = 0; i<5; i++) { //Run five times
  circles[i] = new circle(); //Make a new circle
  stage.addChildAt(circles[i],1); //Layer control, no problem
  circles[i].x = (stageCenterWidth*Math.random()*5)/(Math.random()*10);
  circles[i].y = (stageCenterHeight*Math.random()*5)/(Math.random()*10);
 }
}

stageCenterHeight and stageCenterWidth are just variables containing the center points of the stage so I do not calculate every time.

I make a new circle every time the for loops runs. However, after this function executes a few times I will call another function who's purpose is to play frame 2 of the circles all at the same time.

Question: How can I target the circles that I have now spammed all over the screen so that I can use something like circles.gotoAndPlay(2);?

+1  A: 

It would go something like this:

for each(var myCircle:circle in circles) {
    myCircle.gotoAndStop(2);
}

UPDATE: According to your comment, there are many options (different arrays, etc...) but as far as I can tell from what you've said, I would create a new container for each batch of circles... spawn() would create a new sprite, add it to the scene, and add all circles from that batch inside that sprite... then, you can change circles by batch:

var lastSprite:Sprite = getChildAt(numChildren-1); // get the latest sprite added to "this" (I suggest not using stage.addChild)
for(var i:uint = 0; i < lastSprite.numChildren; i++) { //for each child of that sprite
   lastSprite.getChildAt(i).gotoAndStop(2); //goto 2
}
Cay
Or even immediately in the for loop that's creating the circles if animation can start right away.circles[i].gotoAndPlay(2);
Lieven Cardoen
This has got me started on the right path.The problem is that in this case the gotoAndPlay(2); only works on the last 5 circles that were displayed. I have the latest 5 + 15 on the stage.Any way to target the ones that were displayed on the stage that last few times the for loop had been run?
Structure
I'll update the answer accordingly...
Cay
Thank you, got it!
Structure
A: 

I think you need to traverse the whole display list, look for all the objects of circle class and act accordingly.


function gotoandstopcircles(container:DisplayObjectContainer):void{
  var circ:*;
  for (var i:uint=0; i < container.numChildren; i++){
    circ = container.getChildAt(i);
    if (circ is circle){
      circ.gotoAndStop(2);
    }
    else if(circ is DisplayObjectContainer){
      gotoandstopcircles(circ);
    }
  }
}
Then whenever you want to do gotoandstop just call:
gotoandstopcircles(root);

bhups
+2  A: 

When you want to do things like this, I will give you a tip. Do not add it to the stage directly because there are other DisplayObject bound to also be added directly to the stage and this will make it harder to do what you want to do.

Make a DisplayObjectContainer of your choice, whether it be MovieClip or Sprite. Personally, I would use a sprite to hold your MovieClips.

The reason we move them into their own container is so that we can iterate through all children of a specific object, and perform actions on each child; in your case, play frame 2.

var container:Sprite = new Sprite();

var circles:Array = new Array() //Declare array for displaying multiple objects
function spawn(evt:MouseEvent):void { //MouseEvent trigger, working
    for (var i:int = 0; i<5; i++) { //Run five times
    circles[i] = new circle(); //Make a new circle

    container.addChildAt(circles[i],1); //Layer control, no problem

    circles[i].x = (stageCenterWidth*Math.random()*5)/(Math.random()*10);
    circles[i].y = (stageCenterHeight*Math.random()*5)/(Math.random()*10);
}

}

now to do anything with your clips, you merely reference the parent object, container;

for(var i:int; i < container.numChildren; i++)
{
    MovieClip(container.getChildAt(i)).gotoAndStop(2); //We cast it to MovieClip to tell it what type of child we are manipulating.
}

In my opinion the array is overkill, but you could have just as easily used it to get refernce to each clip as you added each circle reference in the same linear order they get added to the display list, the same for loop, but ran against the array length instead of the container clips numChildren.

Because of the display lists power and bubbling, its nice to create parent clips to put common display items inside of. Say you put ten intended navigation buttons into one clip called nav. You could simply listen for MOUSE_DOWN on nav, then in the handler function distinguish which child was clicked and take action accordingly. This is tremendous as we cut out nine other listeners that potentially would have never been removed.

Hope this helped.

Brian Hodge hodgedev.com

Brian Hodge
+1  A: 

You use the following code:

var circles:Array = new Array() //Declare array for displaying multiple objects
function spawn(evt:MouseEvent):void { //MouseEvent trigger, working
 for (var i:int = 0; i<5; i++) { //Run five times
  circles[i] = new circle(); //Make a new circle
  stage.addChildAt(circles[i],1); //Layer control, no problem
  circles[i].x = (stageCenterWidth*Math.random()*5)/(Math.random()*10);
  circles[i].y = (stageCenterHeight*Math.random()*5)/(Math.random()*10);
 }
}

But in this way you overwrite the references in you circles array every time you call the spawn function. If you want to keep the references to your circle to use them later on you should push the circle on the circles array, like this:

function spawn(evt:MouseEvent):void { //MouseEvent trigger, working
 for (var i:int = 0; i<5; i++) { //Run five times
  var c:circle = new circle(); //Make a new circle
  stage.addChildAt(circle,1); //Layer control, no problem
  c.x = (stageCenterWidth*Math.random()*5)/(Math.random()*10);
  c.y = (stageCenterHeight*Math.random()*5)/(Math.random()*10);
  circles.push(c);
 }
}

And then later on you can use the circles array like this:

function gotoAndStopYourCircles(frameNumber:uint):void {
 for (var c:circle in circles){
  c.gotoAndStop(frameNumber);
 }
}
Vincent Osinga