# Shooter[AS2.0] - from pistol to SMG

This topic is 2549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, so i have this zombie shooter game, everything worked fine, aspecially the bullet firing, but i only used pistols with low fire rate, i decided to add a SMG(SubMachine Gun). 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(same 10bps), but close(~<150px) to the zombies - the zombies get hit. What could be the problem?
The code for the bullets:
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 }

And for the zombies:
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(); } } } } }

##### Share on other sites
ok... not many replies... here's the swf - http://www.megaupload.com/?d=T6JYEXWG. Pickup the black mp5k(smg) on the ground and try shooting the zombies(i reduced their speed to 0) from far and close, rapidly and single shot. You'll see that when shooting rapidly from far - you can't hit them. So i want to know - what could be the problem?

##### Share on other sites
This could be the problem, you only do a collision test for the very last bullet fired.
So from far away, when you fire a single shot from the SMG it works, but multiple shots would not.

 _root["zombie_"+i].onEnterFrame = function(){ if (this.hitTest(bullet)) { // ... } } 

You need to keep an array of all "live" bullets and do a collision test for all of them.
As long as you don't have too many bullets & zombies, just iterate all bullets for each zombie. This is the simplest way, but not best for performance.

##### Share on other sites

This could be the problem, you only do a collision test for the very last bullet fired.
So from far away, when you fire a single shot from the SMG it works, but multiple shots would not.

[quote name='SeriWolk' timestamp='1311414350' post='4839233']
 _root["zombie_"+i].onEnterFrame = function(){ if (this.hitTest(bullet)) { // ... } } 

You need to keep an array of all "live" bullets and do a collision test for all of them.
As long as you don't have too many bullets & zombies, just iterate all bullets for each zombie. This is the simplest way, but not best for performance.
[/quote]

ok, ty, i'll to something about that

##### Share on other sites
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)){ crit=random(10); if(crit==0){ this.removeMovieClip(); }else{ this.enemyHp-=dm; } bullet_arr.removeMovieClip(); if(this.enemyHp<=0){ this.removeMovieClip(); } } } 

##### Share on other sites
Also don't forget to remove the bullet from the array when it's removed from the display list.

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631764
• Total Posts
3002210
×