0

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
Joseph Wagner
  • 303
  • 3
  • 16
  • Not sure what you're asking (not run while the event is not occurring?). Is there a need to check for collision every single frame? – BadFeelingAboutThis Aug 20 '12 at 21:11
  • You can use the Timer class instead of enter frame, then you set the interval of how often you want to check. If this is what you mean let me know and I'll do an example as an answer – BadFeelingAboutThis Aug 20 '12 at 21:15

3 Answers3

0

It's hard to answer this question as it is pretty vague when you're not checking for collision. To improve the performance it's not about changing how to collision test, but when to do the check.

so you could do this by simply adding an if statement like so:

if (doCollisionTest){testCollision();}

but that doesn't solve when doCollisionTest is true or false, and that's the tricky part, that cannot be answered from the information you provided.

Daniel
  • 34,125
  • 17
  • 102
  • 150
0

If you don't need to check every single frame, you can use a Timer to only check as often as you like, for instance, if you deemed it good enough to check 3 times per second, you'd set it up like this:

var timer:Timer = new Timer(333); //run 3 times a second
timer.addEventListneer(TimerEvent.TIMER, collisionTest, false, 0, true);
timer.start();

function collisionTest(e:Event = null):void {
   //do you collision stuffs
}

Then, if for whatever reason you want to temporarily disable it, use timer.stop() , then timer.start() again

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Wouldn't that off-set my collision tests, though? Since it is only being checked three times a second, couldn't those gaps cause problems? My program is currently running on 30 fps(not sure what the best is for a game), so only checking it 10% of the times SEEMS a little loose. – Joseph Wagner Aug 21 '12 at 01:25
  • Yeah, as soon as I implemented the timer, the bullets would pass through the enemies sometimes. I appreciate the help, but for something as precise as a collision test, I'm not sure if the timer will be best...unless you can think of a better way to use it. – Joseph Wagner Aug 21 '12 at 01:42
  • You should be checking then in the same block of code that moves your bullet. If your bullet doesn't move, then don't do the collision test. You didn't give any clear context in your question so this seemed like what you were asking for – BadFeelingAboutThis Aug 21 '12 at 16:12
0

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.

Neil
  • 7,861
  • 4
  • 53
  • 74