For loop problem

I have a for loop that creates pickups, and assigns an onEnterFrame to it:

var pickups = 10;
for (var i = 0; i<pickups; i++) {
    var mc = _root.attachMovie("pickup", "pickup_"+i, i);
    var speed = Math.floor(Math.random()*(20-5))+5;
    mc._x = random(Stage.width);
    mc.onEnterFrame = function() {
        this._y += speed;
    };
}

However this doesn’t work, and the speed isn’t randomized. They are all assigned the same speed. I can’t figure it out. Does anyone know why my code won’t work? I really appreciate it.

Thanks,
motionman95 :thumb2:

It returns a random speed for me. You could just assign a wider range.

Thanks for replying. When I do it, the movieclips on stage move at the same speed. Do you know why?

I have no idea. The logic is fine. I just wrote a quick sample using your code and it’s fine.


var pickups:int = 10;

for (var i:int = 0; i < pickups; i++) 
{
    var curClip:MovieClip = new MovieClip();
		curClip.graphics.beginFill(0x444444);
		curClip.graphics.drawCircle(0, 0, 10);
		curClip.graphics.endFill();
	///
    var speed = Math.floor(Math.random()*(20-5))+5;
	///
	curClip.speed = speed;
	curClip.x = Math.random() * stage.stageWidth;
	curClip.y = Math.random() * stage.stageHeight;
	///
	addChild(curClip);
    curClip.addEventListener(Event.ENTER_FRAME, moveClip);
} 

function moveClip(e:Event):void
{
	e.target.x += e.target.speed;
}