# Firing bullets and hitTest problem

**URL:** <https://forum.kirupa.com/t/firing-bullets-and-hittest-problem/288204>\
**Category:** Uncategorized\
**Created:** [May 12, 2009, 6:59pm UTC](https://forum.kirupa.com/t/firing-bullets-and-hittest-problem/288204 "2009-05-12T18:59:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gencoglu](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gencoglu](https://forum.kirupa.com/u/gencoglu)\
**Post date:** [May 12, 2009, 6:59pm UTC](https://forum.kirupa.com/t/firing-bullets-and-hittest-problem/288204/1 "2009-05-12T18:59:03Z")

</div>

Hi,  
I have the following code to detect enemy within the range and fire…The problem is hitTest does not work and secondly it fires many bullets at a trime  
could you please help me to modify the code …  
thanks…

```php

var radians:Number = Math.PI/180;
var radians2:Number = 180/Math.PI;
loadWeapon("Machine Gun");
function loadWeapon(weaponType:String):Void {
 bulletNumber = 1;
 bulletOffset = 10;
 bulletSpeed = 15;
}
function fireWeapon():Void {
 if (reloadComplete) {
  createBullet();
 }
}
function createBullet():Void {
 for (var i = 0; i<bulletNumber; i++) {
  var tempBullet:MovieClip = _root.attachMovie("bullet", "b"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
  tempBullet._x = weapon_mc._x;
  tempBullet._y = weapon_mc._y;
  var randomNum:Number = random(bulletOffset)-(bulletOffset/2);
  tempBullet.bulletAngle = (weapon_mc._rotation+randomNum)*radians;
  tempBullet.xSpeed = Math.cos(tempBullet.bulletAngle)*bulletSpeed;
  tempBullet.ySpeed = Math.sin(tempBullet.bulletAngle)*bulletSpeed;
  tempBullet.onEnterFrame = function() {
   this._x += this.xSpeed;
   this._y += this.ySpeed;
  };
 }
 startReloading();
}
function startReloading() {
 reloadComplete = false;
}
function gunReloadedx() {
 clearInterval(reloadTimer);
 reloadComplete = false;
}
function gunReloaded() {
 clearInterval(reloadTimer);
 reloadComplete = true;
}
function destroyBullet(bullet:MovieClip):Void {
 clearInterval(bullet.lifeTimer);
 bullet.removeMovieClip();
}
function rotateWeapon() {
 weapon_mc._rotation = Math.atan2(_root.hero._y-weapon_mc._y, _root.hero._x-weapon_mc._x+30)*radians2;
}
_root.onEnterFrame = function() {
 fireWeapon();
 walk();
 rotateWeapon();
};
function walk() {
 _root.hero._x += 2;
 if (_root.hero._x>400) {
  _root.hero._x = 10;
 }
 rangex = _root.weapon_mc._x-_root.hero._x;
 if (rangex>=-100 && rangex<=100) {
  gunReloaded();
 }
 if (_root.bullet.hitTest(_root.hero)) {
  unloadMovie(_root.hero);
 }
}

```
