Copy link to clipboard
Copied
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);
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.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.
Copy link to clipboard
Copied
when you want to remove your zombies, use
function removeZombies():Void{
for(var i:Number=Zombies.length-1;i>=0;i--){
Zombies.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)