A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Having trouble with this piece of code (arrays and addChild)

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    69

    Having trouble with this piece of code (arrays and addChild)

    var myShapes:Array = [tl, tr, bl, br];
    trace(myShapes);

    var long:uint = myShapes.length;
    for (var i:uint = 0; i < long; i++)
    {
    addChild();
    this.x = Math.random() * stage.stageWidth;
    this.y = Math.random() * stage.stageHeight;
    this.vx = 0;
    this.vy = 0;
    this.vz = 0;
    }
    I'm getting an error on the "addChild();" line. I've set up an array made up of different movieclips, now I want to get those movieclips onto the stage and animate them randomly (meaning each one moves around the stage at random with the "animateShapes" function.

    What am I missing here ... I'm guessing it's something really simple.

    ETA: tried "this.addChild(MovieClip(myShapes));" but still getting Error: "Incorrect number of arguments: Expected 1"
    Last edited by Leftyplayer; 09-08-2009 at 06:23 PM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You need
    Code:
    addChild(myShapes[i]);
    Assuming that you actually do have the movieclips in the array. Putting their names in the array instead is a common mistake.

    Also, why would you set the position and velocity each time through the loop? Only the last time through will count. I think maybe you wanted to set the position and velocity of the individual clips you're adding, in which case you wanted:
    Code:
    var myShapes:Array = [tl, tr, bl, br];
    trace(myShapes);
    
    var long:uint = myShapes.length;
    for (var i:uint = 0; i < long; i++){
      var shape:MovieClip = myShapes[i];
      addChild(shape);
      shape.x = Math.random() * stage.stageWidth;
      shape.y = Math.random() * stage.stageHeight;
      shape.vx = 0;
      shape.vy = 0;
      shape.vz = 0;
    }

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    69
    That works ... and makes sense.

    Thank you for your help!

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