# For loop problem

**URL:** https://forum.kirupa.com/t/for-loop-problem/262439
**Category:** Uncategorized
**Created:** [June 5, 2008, 10:02pm UTC](https://forum.kirupa.com/t/for-loop-problem/262439 "2008-06-05T22:02:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 5, 2008, 10:02pm UTC](https://forum.kirupa.com/t/for-loop-problem/262439/1 "2008-06-05T22:02:45Z")

</div>

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

```php
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:

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [June 5, 2008, 10:11pm UTC](https://forum.kirupa.com/t/for-loop-problem/262439/2 "2008-06-05T22:11:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 5, 2008, 10:16pm UTC](https://forum.kirupa.com/t/for-loop-problem/262439/3 "2008-06-05T22:16:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [June 5, 2008, 10:57pm UTC](https://forum.kirupa.com/t/for-loop-problem/262439/4 "2008-06-05T22:57:05Z")

</div>

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

```auto

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;
}

```
