Sign in to follow this  
SeriWolk

Shooter[AS2.0] - from pistol to SMG

Recommended Posts

SeriWolk    100
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:
[code]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
}[/code]

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

Share this post


Link to post
Share on other sites
SeriWolk    100
ok... not many replies... here's the swf - [url="http://www.megaupload.com/?d=T6JYEXWG"]http://www.megaupload.com/?d=T6JYEXWG[/url]. 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 this post


Link to post
Share on other sites
Angex    884
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']
[code]
_root["zombie_"+i].onEnterFrame = function(){
if (this.hitTest(bullet)) {
// ...
}
}
[/code]
[/quote]

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 this post


Link to post
Share on other sites
SeriWolk    100
[quote name='Angex' timestamp='1311503909' post='4839543']
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']
[code]
_root["zombie_"+i].onEnterFrame = function(){
if (this.hitTest(bullet)) {
// ...
}
}
[/code]
[/quote]

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 this post


Link to post
Share on other sites
SeriWolk    100
thank you for putting me on the right track, added a few lines and changes and everything works:
[code]
//...
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();
}
}
}
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this