I'm building my first Flash program, and I need help with a problem with collision detection...
I'm making a side scrolling video game and am trying to tweak my collision detection.
What I currently have:
The "player's"/controlled Movie Clip deducts health from a "health" movie clip when other specified movie clips hit the player. The movie clips hitting the player are then removed. However, the top half of the graphic I'm using (a png with transparent background) is recessed from the lower half, meaning that if an enemy movie clip hits the top half of the bounding box, the result is what looks like a collision with empty space. Here's the basic jist of my current code (I place this in the Actionscript of whatever object I want to collision detection against):
function onEnterFrame()
{
if(this.hitTest(_root.player))
{
this.removeMovieClip();
}
}
What I'm trying to do:
I'd like to draw a transparent circle on the upper portion of the player movie clip, but *within* the player movie clip so that I can run hitTest against that instead of the entire graphic. I've been trying to reference the circle in the hitTest like so: if(this.hitTest(_root.player.collide) but no joy. I also tried referencing the clip directly with (_root.collide). I've also tried both putting the circle on a separate layer and in the same layer within the player movie clip - In all cases, I end up with no collision detection at all. Ultimately, I want the effect of enemy objects only damaging that top, vulnerable part of the player.
What I'd like to avoid:
BitmapData.hitTest
hitTestPoint
I tried to figure these out, but they seemed overly complex for what I want to do. Also, I've still quite newbish and this is my first exposure to ActionScript.
Bonus question: can anyone explain (or point me the the right direction for) when I do and don't need to specify the Identifier and Class for a symbol? Also, any good Flash game programming tutorials that teach by doing and then explaining without laying out the entire groundwork for the language (like,
Kongregate's Shootorials)?
if (detectHit(clipA.x, clipA.y, clipB.x, clipB.y, 50) == true){
//do collision stuff.
}
function detectHit(x1:Number, y1:Number, x2:Number, y2:Number, distance:Number):Boolean{
var distanceX=Math.abs(x1-x2);
var distanceY=Math.abs(y1-y2);
if ((distanceX*distanceX)+(distanceY*distanceY)) < (hitDistance*hitDistance)){
return true; // basic Pythagorean theorem
} else {
return false;
}
}
If you want the hit area to be different than the center point of the movie clip, just add a property to your movie clip with that center,
clipA.hitX=0;
clipA.hitY=-25;
clipA.hitSize=50;
clipB.hitX=0;
clipB.hitY=-25;
clipB.hitSize=50;
if (detectHit(clipA.hitX clipA.hitY, clipB.hitX, clipB.hitY, clipA.hitSize+clipB.hitSize)==true)
... do stuff
Note that this basically code and hasn't been through a compiler and might well throw errors.
posted by Ookseer at 6:43 PM on May 30