1 Reply Latest reply on Dec 22, 2016 6:44 AM by kglad

    Actionscript 2 Bullets movieclip

    xCyper

      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[i].Diffx = _root.player._x-Zombies[i]._x;

        Zombies[i].Diffy = _root.player._y-Zombies[i]._y;

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

        if (Zombies[i].Distance>5) {

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

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

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

        //MAKING THE ZOMBIE DIE WHEN IT IS SHOT

        }if(Zombies[i].hitTest(Bullet)){

        removeMovieClip(Zombies[i]);

        removeMovieClip(Bullet);

        //WHAT HAPPENS WHEN ZOMBIE TOUCHES PLAYER

        }if(Zombies[i].hitTest(player)){

        removeMovieClip(Bullet);

                  _root.health -=.1;

        }

        }

      }

       

      Thank you for your help.

        • 1. Re: Actionscript 2 Bullets movieclip
          kglad Adobe Community Professional & MVP

          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();

          }