PDA

View Full Version : [AS2] From Pistol to SMG


SeriWolk
07-22-2011, 12:52 PM
Bullet:

var bullet:MovieClip;
//+other vars needed below

function bulletFire(){
if(reloadComplete==true){
bullet = attachMovie("bullet", "bullet_"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_b_x, _y:start_b_y});
bullet._rotation=player._rotation;//bullet rotation
bullet.bulletLifeTimer = 0;

//bullet offset
randomNum = Math.random() * bulletOffset - (bulletOffset / 2);
var bulletAngle = player._rotation-90 + randomNum;

bullet.dirx = Math.cos(bulletAngle*Math.PI/180)*firePower;
bullet.diry = Math.sin(bulletAngle*Math.PI/180)*firePower;


bullet.onEnterFrame = function() {
//moving bullet
this._x += this.dirx;
this._y += this.diry;

//checking bullet lifetime
if (this.bulletLifeTimer>=bulletLifeTimerTotal) {
this.removeMovieClip();//delete the bullet
}

this.bulletLifeTimer++; };
}

startReloading();
}
}

function startReloading()
{
reloadComplete = false;//reload started
reloadTimer = setInterval(gunReloaded, reloadSpeed);//pistol - 350, smg - 100
}

function gunReloaded()
{
clearInterval(reloadTimer);//clear timer
reloadComplete = true;//reload complete
}


Zombie:

function enemy(){
for(i=1;i<=20;i++){
if(enemySpawned==false){
if(i<20){
attachMovie("zombie","zombie_"+i,100+i,{_x:random(800),_y:random(800),enemyHp:10 });
}
}
if(i==20){
enemySpawned=true;
}
_root["zombie_"+i].onEnterFrame = function(){
if(this.hitTest(bullet)){
crit=random(10);
if(crit==0){
this.removeMovieClip();//10% of headshot
}else{
this.enemyHp-=dm;
}
bullet.removeMovieClip();
if(this.enemyHp<=0){
this.removeMovieClip();
}
}
}
}
}


The code is simplified to the max, so the problem occured when i added a smg - the bullets started to ignore the zombies, they fly right through them, but only if shooting in auto mode(10 bullets in 1 second), if i shoot 1-2 bullets at a time or shoot rapidly, but close(~<150px) to the zombies - the zombies get hit. What could be the problem?

SeriWolk
07-24-2011, 08:58 AM
ok... not many replies... here's the swf - megaupload.com/?d=T6JYEXWG. Pickup the black mp5k(smg) on the ground and try shooting the zombies(i reduced their speed to 0) from long and close range, rapid and single shot. You'll see that when shooting rapid from long range - you can't hit them. So i want to know - what could be the problem?

neilmmm
07-24-2011, 08:43 PM
I think the problem is bullet

You declare it outside the function but if you have 2 or more on stage flash will be confused

So either push bullet to an array and hitTest zombies to the array with a for loop or hitTest to zombies in the function you attached bullet in using this to ref bullet

However as I think you have more than one zombie then you have the same problem doubled and must use an array for one or both.

As you remove clips you will have to splice out ref to optimise

Hope it helps

SeriWolk
07-28-2011, 05:35 AM
I think the problem is bullet

You declare it outside the function but if you have 2 or more on stage flash will be confused

So either push bullet to an array and hitTest zombies to the array with a for loop or hitTest to zombies in the function you attached bullet in using this to ref bullet

However as I think you have more than one zombie then you have the same problem doubled and must use an array for one or both.

As you remove clips you will have to splice out ref to optimise

Hope it helps
well, ty, now i know whats the problem, i'll try to solve it somehow

SeriWolk
07-28-2011, 06:08 AM
thank you for putting me on the right track, added a few lines and changes and everything works:

//...
bullet = attachMovie("bullet", "bullet_"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_b_x, _y:start_b_y});
bullet_arr.push(bullet);
//...

for(i=0;i<=bullet_arr.length;i++){
if(this.hitTest(bullet_arr[i])){
crit=random(10);
if(crit==0){
this.removeMovieClip();
}else{
this.enemyHp-=dm;
}
bullet_arr[i].removeMovieClip();
if(this.enemyHp<=0){
this.removeMovieClip();
}
}
}