# problem: ENTER\_FRAME event works only on one object

**URL:** <https://forum.kirupa.com/t/problem-enter-frame-event-works-only-on-one-object/634756>\
**Category:** flash\
**Created:** [June 12, 2016, 3:13pm UTC](https://forum.kirupa.com/t/problem-enter-frame-event-works-only-on-one-object/634756 "2016-06-12T15:13:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tzvika\_Frenkel](https://avatars.discourse-cdn.com/v4/letter/t/ac91a4/32.png) [@Tzvika\_Frenkel](https://forum.kirupa.com/u/Tzvika_Frenkel)\
**Post date:** [June 12, 2016, 3:13pm UTC](https://forum.kirupa.com/t/problem-enter-frame-event-works-only-on-one-object/634756/1 "2016-06-12T15:13:27Z")

</div>

Hi, i’m trying to make the circles i have in my game move around, each one in different direction, but the ENTER\_FRAME works only on one circle…

can you help me understand why the loop doesn’t work properly and only one circle is moving around?  
thanks 🙂

var xDirection: String = “right”;  
var yDirection: String = “down”;  
var speed: Number;

for (var i: Number = 0; i \< 10; i++) {  
var badGuy1: badGuy = new badGuy();  
badGuy1.x = GetRandomXPosition();  
badGuy1.y = GetRandomYPosition();  
/_speed = Math.round(.5 + Math.random() \* 5);_/  
addChild(badGuy1);  
addEventListener(Event.ENTER\_FRAME, MoveBadGuys);  
}

// Good guy definition---------------------------------------

var goodGuy1: goodGuy = new goodGuy();  
goodGuy1.addEventListener(Event.ENTER\_FRAME, goodGuyMovement);  
this.addChild(goodGuy1);

function goodGuyMovement(event: Event): void {  
Mouse.hide();  
goodGuy1.x = mouseX;  
goodGuy1.y = mouseY;  
}

//------------------------------------------------------------

function GetRandomXPosition(): Number {  
return Math.floor(Math.random() \* stage.stageWidth);  
}

function GetRandomYPosition(): Number {  
return Math.floor(Math.random() \* stage.stageHeight);  
}

addEventListener(Event.ENTER\_FRAME, MoveBadGuys);  
function MoveBadGuys(event: Event): void {  
if (badGuy1.x \> stage.stageWidth) {  
xDirection = “left”;  
} else if (badGuy1.x \< 0) {  
xDirection = “right”;  
}

```
if (badGuy1.y > stage.stageHeight) {
	yDirection = "up";
} else if (badGuy1.y < 0) {
	yDirection = "down";
}

if (xDirection == "right") {
	badGuy1.x += 20;
} else {
	badGuy1.x -= 20;
}

if (yDirection == "up") {
	badGuy1.y -= 20;
} else {
	badGuy1.y += 20;
}

```

}

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 12, 2016, 11:05pm UTC](https://forum.kirupa.com/t/problem-enter-frame-event-works-only-on-one-object/634756/2 "2016-06-12T23:05:38Z")

</div>

Your listener only references badGuy1, which will point to the last instance of badGuy created in the loop. Probably the best solution, at least in my opinion, would be to incorporate the movement code into the badGuy class. Alternatively, you could store all of the badGuys in an array and loop through them all in the enterFrame listener.

---

<div class="post-metadata">

**Author:** ![Tzvika\_Frenkel](https://avatars.discourse-cdn.com/v4/letter/t/ac91a4/32.png) [@Tzvika\_Frenkel](https://forum.kirupa.com/u/Tzvika_Frenkel)\
**Post date:** [June 13, 2016, 4:34am UTC](https://forum.kirupa.com/t/problem-enter-frame-event-works-only-on-one-object/634756/3 "2016-06-13T04:34:02Z")

</div>

thank you 😄

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 14, 2016, 1:03am UTC](https://forum.kirupa.com/t/problem-enter-frame-event-works-only-on-one-object/634756/4 "2016-06-14T01:03:37Z")

</div>

You’re welcome.
