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…
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);
}
}