# Tower Defense - Shooting

**URL:** <https://forum.kirupa.com/t/tower-defense-shooting/289373>\
**Category:** programming\
**Created:** [May 29, 2009, 3:20pm UTC](https://forum.kirupa.com/t/tower-defense-shooting/289373 "2009-05-29T15:20:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewey](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@dewey](https://forum.kirupa.com/u/dewey)\
**Post date:** [May 29, 2009, 3:20pm UTC](https://forum.kirupa.com/t/tower-defense-shooting/289373/1 "2009-05-29T15:20:12Z")

</div>

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.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.walterreid.com/blog/56/how-to-build-a-tower-defense-flash-game-part-4-starting-the-tower-attack)

> **[Tower Defense in AS3 – Part IV](http://www.guahanweb.com/2009/01/31/tower-defense-in-as3-part-iv/)**
>
> Many of you have been waiting patiently for this post, and let me say that I have worked many hours to get this where it is today. I wanted to get my code to the point where it was solid enough tha…

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…

```auto
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…

```auto
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…

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

```

Thanks everyone!

–d
