...

View Full Version : shooting game and hittest



gencoglu
05-07-2009, 11:01 AM
Hi,
I am tring to create a shooting game to shoot a moving object from a fixed target(a tower).
I found the following code on internet and tried to modified it as much as I could..
the frst problem is that when hitTest occurs it does not kill and unload the movie..!!!
the second thing I could not figure out was how to stop/start firing when enemy is within or out of range which

is say 50 pixel..
if anyone out there help me, ı will be thankfull..

thanks in advance.







var bulletNumber:Number;
var bulletOffset:Number;
var bulletSpeed:Number;
var bulletMaxAge:Number;
var reloadSpeed:Number;
var reloadTimer:Number;
var reloadComplete:Boolean = true;
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 = 8;
bulletMaxAge = 1000;
reloadSpeed = 700;
}
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.lifeTimer = setInterval(destroyBullet, bulletMaxAge, tempBullet);
tempBullet.onEnterFrame = function() {
this._x += this.xSpeed;
this._y += this.ySpeed;
};
}
startReloading();
}
function startReloading() {
reloadComplete = false;
reloadTimer = setInterval(gunReloaded, reloadSpeed);
}
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)*radians2;
}
_root.onEnterFrame = function() {
fireWeapon();
walk();
rotateWeapon();
mouseClicked = true;
};
//b1_btn.onRelease = function() {
//loadWeapon("Machine Gun");
//};
function walk() {
//*************************
_root.hero._x += 2;
if (_root.hero>400) {
_root.hero = 10;
}
if (_root.bullet.hitTest(_root.hero)) {
unloadMovie(_root.hero);
}
//**************************************
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum