actionscript-3enterframeevent

AS3 - stop onEnterFrame Function from constantly running


Okay, I have a collisionTest function that is tied to my "onEnterFrameHandler" function.

So to simplify the way it looks:

onEnterFrameHandler(e:Event):void{
testCollision();
}

testCollision():void{
  trace("always running");
  if(1_MC.hitTestObject(2_MC)){
    //do stuff
  }
}

The thing is, it is always running. Constantly running in order to test for a collision. I have a feeling that it is what may be causing the lag on this project.

Do you know of a good way to control a function that needs to be able to check, at any time, an event, yet not run while the even is not occurring?


Solution

  • Usually you are always checking for collisions in a game loop. You provide a flag to indicate if something needs to be checked or not.

    On a side note, consider using collision detection kit, I think you'll find it takes care of just about every scenario you can have regarding collisions.