So I’m working on my first game. It’s a top-down zombie shooter, basically. Nothing special, just getting used to AS3.
I’m trying to develop a hit test method for my game that has the player shoot a line out from his gun, the length of the line being the gun range, and if that line collides with a square hitbox (drawn inside the zombie movieclip symbol) then it fires the hit test. The problem is, when I draw the line as a sprite, I’m pretty sure that it’s creating a rectangular bounding box around the drawn line, thus screwing up the hit test.
Here’s my code for the entire shooting function so far:
private function shootBullet():void {
//get global coordinates of gun
var linePoint:Point = new Point(player.player_gun.x, player.player_gun.y);
var gunX:int = player.localToGlobal(linePoint).x;
var gunY:int = player.localToGlobal(linePoint).y;
//set start point 10 px from registration point of gun w/angle of gun
var radius:int =- 18;
var gunRange:uint = 500;
var lineStartX:Number = gunX + radius * Math.cos(bullet_angle);
var lineStartY:Number = gunY + radius * Math.sin(bullet_angle);
var lineEndX:int = lineStartX - gunRange * Math.cos(bullet_angle);
var lineEndY:int = lineStartY - gunRange * Math.sin(bullet_angle);
//var lineBullet:LineBullet = new LineBullet(lineStartX, lineStartY, lineEndX, lineEndY);
var lineBullet:Sprite = new Sprite();
parent.addChild(lineBullet);
var g:Graphics = lineBullet.graphics;
g.lineStyle(0, 0x000000);
var startX:int = lineStartX;
var startY:int = lineStartY;
var endX:int = lineEndX;
var endY:int = lineEndY;
g.moveTo(startX, startY);
g.lineTo(endX, endY);
if(lineBullet.hitTestPoint(zombie.x, zombie.y, true)) {
trace("Zombie Hit");
} else {
trace("Missed Zombie");
}
}
It’s spaghetti code right now, I know. Also I never finished commenting xD. I will soon be distributing the snippets of codes into proper classes, but for now I just want to get this working.
Can anyone suggest a better way to go about doing this? I want to keep my draw-a-line method. I’m going to implement some small timers to simulate bullet speed and such, so I don’t actually have to draw/animate bullets.
Thanks,
Nate