1 Reply Latest reply on Apr 15, 2017 3:18 PM by kglad

    Need help fixing a glitch

    ssv34458059 Level 1

      Hello,

      For school I'm making a 2D top-view zombie shooter game but there's a big glitch that I need to fix.

      There are 2 levels, when a player reaches a certain point they go to the next level (the next frame).

      But when you enter the second level, the zombies from the first level stay on the screen, not moving and they can't be killed.

      Here is my code, this is an old code so don't look at the other stuff, that's unnecessary.

       

      //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)

        removeMovieClip(Zombies[i]);

        removeMovieClip(Bullet);

       

        }

        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: Need help fixing a glitch
          kglad Adobe Community Professional & MVP

          when you want to remove your zombies, use

           

           

          function removeZombies():Void{

          for(var i:Number=Zombies.length-1;i>=0;i--){

          Zombies[i].removeMovieClip();

          }

          Zombies.length=0;

          }

           

          p.s.  you should be looping through your Zombies array in reverse order in any loop where you remove zombie elements from Zombies. (eg, in ZombieAct)