# OnEnterFrame and lag

**URL:** <https://forum.kirupa.com/t/onenterframe-and-lag/295299>\
**Category:** programming\
**Created:** [August 27, 2009, 8:30pm UTC](https://forum.kirupa.com/t/onenterframe-and-lag/295299 "2009-08-27T20:30:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuperSkyler](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@SuperSkyler](https://forum.kirupa.com/u/SuperSkyler)\
**Post date:** [August 27, 2009, 8:30pm UTC](https://forum.kirupa.com/t/onenterframe-and-lag/295299/1 "2009-08-27T20:30:04Z")

</div>

I’m creating a basic game in actionscript 2. Essentially the user will control a canon at the bottom of the stage with the left and right arrow keys. They will fire canons at these “germs” floating around the screen. The germs move in a linear motion, and will go in the opposite direction once they touch the boundary of the stage. If one of the germs happens to touch the players cannon, they lose.

Here is the bulk of the code:

```auto

var germ_mov:Array = new Array ();
 
for (i = 0; i < MAX_GERMS; i++) {
     germ_mov * = new Object ();
 
     /* Germ is the instance name given to the movieclip containing the germ graphic, boundary is the instance name given to the movieclip containing the perimeter in which the game is contained in */
     germ.duplicateMovieClip ("germ"+i, i, {_x:Math.random () * boundary.width, _y:Math.random () * boundary.height});
 
     germ_mov *.dir_x = Math.round (Math.random () * 3);
     germ_mov *.dir_y = Math.round (Math.random () * 3);
}
 
onEnterFrame = function () {
     for (j = 0; j < MAX_GERMS; j++) {
          var cur_germ:String = "germ"+i;
          updatePos (cur_germ, j);
          checkDir (cur_germ, j);
     }
}
 
function updatePos (germ_guy, num) {
     this[germ_guy]._x += germ_mov [num].dir_x;
     this[germ_guy]._y += germ_mov [num].dir_y;
}
 
function checkDir (germ_guy, num) {
     if ((this[germ_guy]._x + this[germ_guy]._width) >= boundary.width || this[germ_guy]._x <= 0) {
          germ_mov[num].dir_x *= -1;
     }
 
     if ((this[germ_guy]._y + this[germ_guy]._height) >= boundary.height || this[germ_guy]._y <= 0) {
          germ_mov[num].dir_y *= -1;
     }
}
 

```

All this does is position 5 germ movieclips somewhere randomly on the screen, and move their \_x and \_y’s using the onEnterFrame loop. Trouble is there is terrible lag. I will need to add more function calls and logic checks once I put in the cannon. How can I improve the speed here? Currently the frame rate is at 30fps. Is there a better way to implement this?
