Skip to main content
xCyper
Known Participant
December 22, 2016
Question

Actionscript 2 Bullets movieclip

  • December 22, 2016
  • 1 reply
  • 983 views

Hello,

I'm working on a zombie shooter game and I've got a problem.

I know it may sound stupid but every time I shoot, the most recent bullet disappears.

Does anyone know a fix for this?

my code is:

//VARIABLES

var playerSpeed:Number = 5;

var BulletArray = new Array();

_root.health = 10

//ATTACHING MOVIECLIPS TO THE SCREEN

attachMovie("player", "player", _root.getNextHighestDepth())

player._x = 275;

player._y = 200;

//PLAYER MOVEMENT

function onEnterFrame(){

  ZombieAct();

  ZIndex++

  if(_root.health<0){

  _root.gotoAndStop(1)

  }

  if(Key.isDown(Key.CONTROL)) {

    var playerSpeed:Number = 7;

    }

    else {

  var playerSpeed:Number = 5;

  }

  if(Key.isDown(Key.UP) || Key.isDown(87)){

  player._y -=playerSpeed;

  player.gotoAndStop(2);

  }else if(Key.isDown(Key.DOWN) || Key.isDown(83)){

  player._y +=playerSpeed;

  player.gotoAndStop(2);

  }if(Key.isDown(Key.RIGHT) || Key.isDown(68)){

  player._x +=playerSpeed;

  player.gotoAndStop(2);

  }else if(Key.isDown(Key.LEFT) || Key.isDown(65)){

  player._x -=playerSpeed;

  player.gotoAndStop(2);

  }

  if (!Key.isDown(Key.LEFT) and (!Key.isDown(65) and (!Key.isDown(Key.RIGHT) and (!Key.isDown(68) and(!Key.isDown(Key.DOWN) and (!Key.isDown(83) and (!Key.isDown(Key.UP) and (!Key.isDown(87))))))))) {

  player.gotoAndStop(1);

  }

}

//ROTATE THE player WITH THE MOUSE

  player.onMouseMove = function(){

  var x:Number = _xmouse-this._x;

  var y:Number = _ymouse-this._y;

  var angleRad:Number = Math.atan2(y, x);

  var angleDeg:Number = angleRad/Math.PI*180;

  this._rotation = angleDeg;

}

//SHOOT BULLETS FROM THE GUN

function ShootBullet(){

  attachMovie("bullet", "Bullet", _root.getNextHighestDepth());

  Bullet.push(Bullet);

  Bullet._x = player._x;

  Bullet._y = player._y;

  Bullet._rotation = _root.player._rotation;

  Bullet.onEnterFrame = function(){

  Bullet._x += Math.cos(Bullet._rotation*(Math.PI/180))*25;

  Bullet._y += Math.sin(Bullet._rotation*(Math.PI/180))*25;

  }

}

//SHOOT BULLET WHEN CLICKING THE MOUSE

onMouseDown = function(){

  ShootBullet();

}

//SET TIME FOR EACH ZOMBIE TO APPEAR ON STAGE

setInterval(CreateZombie, 2500);

//ZOMBIE VARIABLE

var Zombies:Array = new Array();

var ZbloodSpill:Number = 1;

var ZbloodArray = new Array();

var ZIndex:Number = 900;

//Maak Zombie

function CreateZombie() {

  z = _root.attachMovie("Zombie", "Zombie"+ZIndex, ZIndex, _root.getNextHighestDepth());

  z._x = Math.random()*600 +100;

  z._y = Math.random()*600 +100;

  z.Diffx;

  z.Diffy;

  z.Distance;

  Zombies.push(z);

}

//MAKE THE ZOMBIE MOVE AND FOLLOW THE PLAYER

function ZombieAct() {

  for (i=0; i<Zombies.length; i++) {

  Zombies.Diffx = _root.player._x-Zombies._x;

  Zombies.Diffy = _root.player._y-Zombies._y;

  Zombies.Distance = Math.sqrt(Zombies.Diffx*Zombies.Diffx+Zombies.Diffy*Zombies.Diffy);

  if (Zombies.Distance>5) {

  Zombies._x += Math.cos(Math.atan2(Zombies.Diffy, Zombies.Diffx))*1.5;

  Zombies._y += Math.sin(Math.atan2(Zombies.Diffy, Zombies.Diffx))*1.5;

  Zombies._rotation = Math.atan2(Zombies.Diffy, Zombies.Diffx)/Math.PI*180;

  //MAKING THE ZOMBIE DIE WHEN IT IS SHOT

  }if(Zombies.hitTest(Bullet)){

  removeMovieClip(Zombies);

  removeMovieClip(Bullet);

  //WHAT HAPPENS WHEN ZOMBIE TOUCHES PLAYER

  }if(Zombies.hitTest(player)){

  removeMovieClip(Bullet);

            _root.health -=.1;

  }

  }

}

Thank you for your help.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 22, 2016

1.  make sure that code is on the _root timeline, or change _root.getNextHighestDepth() to thecurrenttimeline.getNextHighestDepth()

2. assign each bullet a different instance name.  eg,

//SHOOT BULLETS FROM THE GUN

function ShootBullet(){

attachMovie("bullet" "Bullet"+_root.getNextHighestDepth(), _root.getNextHighestDepth());

Bullet.push(Bullet);

Bullet._x = player._x;

Bullet._y = player._y;

Bullet._rotation = _root.player._rotation;

Bullet.onEnterFrame = function(){

Bullet._x += Math.cos(Bullet._rotation*(Math.PI/180))*25;

Bullet._y += Math.sin(Bullet._rotation*(Math.PI/180))*25;

}

}

//SHOOT BULLET WHEN CLICKING THE MOUSE

onMouseDown = function(){

ShootBullet();

}