I'm new to flash and programming in general but am learning it to make games. I'm currently messing around with hit detection and its not to hard to test in the game tick ( fired from onEnterFrame).
What I'm wondering is if it would be possible / useful to create a custom event that i can listen for. And make the eventListener hear when a collision happens.
Also would this be better or worse for the cpu than testing the collisions of lots of enemies on a screen?
Cheers, Tyler
Marty Wallace gave you an exhaustive example of how you might implement custom events for a physics engine. Now I'm going to attempt to weigh in on the question of whether it's worth the strain on the CPU :
The answer is a definite... "Maybe?"
First thing to remember is that an event system like the one implemented in Flash is heavy weight. Here's what happens implicitly when an event is dispatched:
So, if you have a system set up such that many collisions may happen and notify many listeners then you have many function calls and many instantiations happening inside your update loop. That's bad. How bad? I like to point to www.jacksondunstan.com for examples of how to optimize AS3. He explains in detail what happens inside the player when you perform action X, and supplies some good benchmarks for all his tests.
If you don't feel like reading his entire blog though, the problem with all these instantiations is that they'll generate a lot of short-lived garbage that will provoke frequent GC runs. You want to avoid that if at all possible in a performance-intensive game. Other things you might want to avoid would be string concatination, or convenience functions that return new object instances as a side-effect (localToGlobal for instance).
At that point, the question is "what constitutes 'performance-intensive'"?
That's really something you'll have to determine for yourself.
Personally I have mostly abandoned the Flash event system. When I need to notify objects of events, I'll do so directly. I rarely need the overhead of the native event system in the context of a game.