PDA

View Full Version : Tower Defense - Shooting



dewey
May 29th, 2009, 11:20 AM
Hey Gang,

I'm having a ton of issues at the moment trying to figure out how to get the firing done for my Tower Defense game.

I've been looking at 3 tutorials for the most part... Taking pieces of each to try and build my own...

http://www.emanueleferonato.com/2007/11/06/make-a-flash-game-like-flash-element-tower-defense-part-2/
http://www.walterreid.com/blog/56/how-to-build-a-tower-defense-flash-game-part-4-starting-the-tower-attack
http://www.guahanweb.com/2009/01/31/tower-defense-in-as3-part-iv/

The first two are in AS2, the 3rd is AS3.

I'm building my game in AS3, but I'm trying to have it much more class based... this is where I'm running into my problems.

I have the Tower working fine, when a creep enters the range of the tower, it knows it should start firing at it... Here's the code for my Bullet class...


package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;

public class Bullet extends Sprite
{
private var _target:Creep;
private var _speed:int;
private var _direction:Number;

public function Bullet(creep:Creep, bulletSpeed:int)
{
super();

_speed = bulletSpeed;
_target = creep;

this.addEventListener(Event.ADDED_TO_STAGE, _init);
}

private function _init(e:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, _init);
this.addEventListener(Event.ENTER_FRAME, _trackBullet);
}

private function _trackBullet(e:Event):void
{

}

private function _destroyMe():void
{
this.removeEventListener(Event.ENTER_FRAME, _trackBullet);
this.parent.removeChild(this);
}

}
}

The closest I can come to get it working is the following which goes inside the _trackBullet function...


var distPoint:Point = localToGlobal(new Point(this.x, this.y));
var distX:Number = _target.x - distPoint.x;
var distY:Number = _target.y - distPoint.y;
var distance:Number = Math.abs(Math.sqrt((distX * distX) + (distY * distY)));
var ratio:Number = _speed / distance;

if (distance < _speed)
{
_target.hit();
_destroyMe();
}
else
{
this.x += Math.round(distX * ratio);
this.y += Math.round(distY * ratio);
}

But when I use this code, I'm getting the issue where the bullet isn't actually getting that close to the creep and it's saying that it's hitting it. If I increase my tower range, it's as if the bullet flies maybe 100 pixels, "hits" the creep 500 pixels away and it executes the "hit" code on the creep... can anyone help me out? Really I'm looking for an explanation on how to code this, I'm having major issues here...

For reference, here's what happens when my Tower object fires at a creep...


private function _fire(e:TimerEvent):void
{
var b:Bullet = new Bullet(_currentTarget, 8);
this.addChild(b);
}

Thanks everyone!

--d

rrh
June 1st, 2009, 12:24 PM
I think your problem is this:
ActionScript Code:

localToGlobal(new Point(this.x, this.y));





You're using the bullet's location, and then transferring from the bullet's coordinates to the global coordinates.

One of these will work better:
ActionScript Code:

localToGlobal(new Point(0,0)); //this is what I would use
parent.localToGlobal(new Point(this.x, this.y)); //I wouldn't really use this, because it's slower, but it is logically equivalent, and helps illustrate the problem

Gadu
June 4th, 2009, 07:07 AM
Thanks ;)

flyingmonkey456
June 7th, 2009, 11:45 AM
well, i don't really know as3, but i know as2 pretty well and it's pretty much transferrable. what exactly is your problem? you didn't say anything specific, just that you were having a ton of problems.