Skip to main content
December 18, 2009
Answered

moving objects in an array

  • December 18, 2009
  • 3 replies
  • 500 views

First off I have 6 boxes of random width and height falling at random x positions.I can't get them to each fall at random speeds. I dono what I did but now they fall at an overal same speed but they speed up and slow down over a fraction of a second. here's what I have

var rArray:Array = new Array();


for (var i = 0; i<6; i++)

{

var rect = new box();

addChild(rect);

rect.x = Math.random() * (stage.stageWidth - 150);

rect.y=-200;

rect.width = Math.floor(Math.random() * (400-150) + 150);

rect.height = Math.floor(Math.random() * (400-150) + 150);

rArray.push(rect);

}



stage.addEventListener(Event.ENTER_FRAME,fallDown,false,0,true);


function fallDown(e:Event):void {

for (var i = 0; i<rArray.length/2; i++) {

rArray.y += 20;

}

for (var n = rArray.length/2; n<rArray.length; n++) {

rArray.y += 14;

}

}

if (rArray.y>stage.stageHeight) {

rArray.y=-200;

}

second how would I go about deleting them so once they fall off the stage they respawn with different dimensions?

This topic has been closed for replies.
Correct answer kglad

then your parameters are mis-defined.  (which looks likely given those widths and heights.)

try:

var rArray:Array = new Array();
var minspeed:uint = 2;
var maxspeed:uint = 10;

for (var i = 0; i<6; i++) {
    var rect = new box();
    rect.speed = minspeed+(maxspeed-minspeed)*Math.random();// define minspeed and maxspeed
    addChild(rect);
    rect.width = Math.floor(Math.random() * (40-15) + 15);
    rect.height = Math.floor(Math.random() * (40-15) + 15);
    rect.x = Math.random() * (stage.stageWidth - rect.width);
    rect.y=-200;
    rArray.push(rect);
}

stage.addEventListener(Event.ENTER_FRAME,fallDown,false,0,true);

function fallDown(e:Event):void {
    for (var i = 0; i<rArray.length; i++) {
        rArray.y += rArray.speed;
        if (rArray.y>stage.stageHeight) {
            rArray.y=-200;
            rArray.speed = minspeed+(maxspeed-minspeed)*Math.random();// define minspeed and maxspeed
            rArray.width = Math.floor(Math.random() * (40-15) + 15);
            rArray.height = Math.floor(Math.random() * (40-15) + 15);
            rArray.x = Math.random() * (stage.stageWidth - rArray.width);
        }
    }
}

3 replies

December 18, 2009

When you create the box give it a delta y (or dy) value to indicate how you want it to fall;

rect.dy = Math.random()*10+10;

Then the fallDown function should just be this:

function fallDown(e:Event):void {

    for (var i = 0; i<rArray.length; i++) {

       rArray.y += rArray.dy;

     if( rArray.y > 400) {

         rArray.x = Math.random()*400;

         rArray.y= 0;

     }

    }

}

December 18, 2009

Yes KGlad, the fla file is simply a white box on a black background which i turned into movie clip and exported for action script. I tried both Ned's and KGlad's example and both have the same error. It's hard to explain what's going on but the squares fall maybe 200 pixels out of a maximum of 800 and stop, then new squares pop up at the top and after a few second there's tons of squares of different shapes.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 18, 2009

then your parameters are mis-defined.  (which looks likely given those widths and heights.)

try:

var rArray:Array = new Array();
var minspeed:uint = 2;
var maxspeed:uint = 10;

for (var i = 0; i<6; i++) {
    var rect = new box();
    rect.speed = minspeed+(maxspeed-minspeed)*Math.random();// define minspeed and maxspeed
    addChild(rect);
    rect.width = Math.floor(Math.random() * (40-15) + 15);
    rect.height = Math.floor(Math.random() * (40-15) + 15);
    rect.x = Math.random() * (stage.stageWidth - rect.width);
    rect.y=-200;
    rArray.push(rect);
}

stage.addEventListener(Event.ENTER_FRAME,fallDown,false,0,true);

function fallDown(e:Event):void {
    for (var i = 0; i<rArray.length; i++) {
        rArray.y += rArray.speed;
        if (rArray.y>stage.stageHeight) {
            rArray.y=-200;
            rArray.speed = minspeed+(maxspeed-minspeed)*Math.random();// define minspeed and maxspeed
            rArray.width = Math.floor(Math.random() * (40-15) + 15);
            rArray.height = Math.floor(Math.random() * (40-15) + 15);
            rArray.x = Math.random() * (stage.stageWidth - rArray.width);
        }
    }
}

kglad
Community Expert
Community Expert
December 18, 2009

if your box class extends the movieclip class, you can use:


var rArray:Array = new Array();


for (var i = 0; i<6; i++)

{

var rect = new box();

rect.speed = minspeed+(maxspeed-minspeed)*Math.random(); // define minspeed and maxspeed

addChild(rect);

rect.x = Math.random() * (stage.stageWidth - 150);

rect.y=-200;

rect.width = Math.floor(Math.random() * (400-150) + 150);

rect.height = Math.floor(Math.random() * (400-150) + 150);

rArray.push(rect);

}



stage.addEventListener(Event.ENTER_FRAME,fallDown,false,0,true);


function fallDown(e:Event):void {

for (var i = 0; i<rArray.length; i++) {

rArray.y += rArray.speed;

if (rArray.y>stage.stageHeight) {

rArray.y=-200;

rArray.speed = minspeed+(maxspeed-minspeed)*Math.random(); // define minspeed and maxspeed

rArray.width = Math.floor(Math.random() * (400-150) + 150);

rArray.height = Math.floor(Math.random() * (400-150) + 150);

rArray.x = Math.random() * (stage.stageWidth - 150);

}

}

}

Ned Murphy
Legend
December 18, 2009

Here's a little different version of what you've done.  It doesn't destroy anything, just recycles them.  The speed for each is different for each cycle and I have it set to be between 5 and 20, but you can adjust that as you like.

var rArray:Array = new Array();

for (var i = 0; i<6; i++)
{
     var rect = new box();
     rArray.push(rect);
     situateBox(rect);
}

stage.addEventListener(Event.ENTER_FRAME,fallDown,false,0,true);

function situateBox(rect:box):void {
     rect.x = Math.random() * (stage.stageWidth - 150);
     rect.y=-200;
     rect.width = Math.floor(Math.random() * (400-150) + 150);
     rect.height = Math.floor(Math.random() * (400-150) + 150);
     rect.speed = 5 + Math.random()*15;
     addChild(rect);
}

function fallDown(e:Event):void {
     for (var i = 0; i<rArray.length; i++) {
            rArray.y += rArray.speed;
            if(rArray.y > stage.stageHeight){
                  situateBox(rArray);
            }
     }
}