Okay, so in Leap of Faith you have to use the arrow keys to move Peter to Jesus while at the same time use the mouse to keep his Faith-o-meter balanced. Not too difficult I think, so throwing some obstacle's in there sounds like a good idea. In an early concept drawing, SubC drew some lighting and sharks. Hmm, I think I can make some periodic lighting bolts, so the user can't just walk straight across the screen, but would have to wait and time for the lighting.
I started researching Collision Detection because obviously I need to know when the lightening intersects with Peter. AS3 has a couple of built in methods to the DisplayObject class that can let you do some rudimentary Collision Detection:
- hitTestObject - Pass in another object reference, and it will tell you if they overlap
- hitTestPoint - Pass in x,y coordinates and it will tell you if the object overlaps or intersects it
At first I thought hitTestObject was the way to go. But it turns out it only looks at an objects Bounding Box, not the object itself. Bad for cases when your object is not a rectangle, hehe.
hitTestPoint is more precise, but I lack the mathematical wizardry to determine the outline of the vectored shape, convert each segment of that to a coordinate, then do the same for the second object, and see if there is an intersect.
A quick note, if you're working with non vector graphics, you'll want to use the BitmapData class's hitTest method to find out if there is an intersection between a bitmap and a point (like hitTestPoing above), or two bitmaps (like hitTestObject). There is a caveat to this though, in that it does not take into account any "stretching, rotation, or other transformations" of either object. That means you can't rotate your bitmaps and expect to get accurate results.
I won't be doing any transformations, but I'm working with vector graphics, but I want more precision than seeing if the objects' bounding boxes collide. I came across a few Collision Detection libraries/classes, and I settled on the Collision Detection Kit (CDK) developed by Corey O'Neil. It's pretty small and lightweight (20k), easy to use with good documentation/examples, and feature rich. What more could you ask for?
I was able to tweak my sprite animation test in a couple minutes to work with the CDK, after quickly replacing the bitmapped Brian with a vectored circle. check out the quick'n'dirty results below (again, click on the flash and use left/right arrow key to navigate)