Tower Defense - Shooting

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

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