Removing enemies. Can't get it right.
well. i've been working on since added second level game. when go level 1 , level 2 , did not kill enemies on stage. stay on stage , new enemies (i believe removed temporarly) added on top of old.
the enemies in game stored in array called enemylist. i've tried emptying it, didn't work. i've tried every method think of, came forums.
my current game code (doesn't include class files enemy, bumper , bullet):
import flash.geom.point; stop(); var leftpressed:boolean = false; var rightpressed:boolean = false; var uppressed:boolean = false; var downpressed:boolean = false; var leftbumping:boolean = false; var rightbumping:boolean = false; var upbumping:boolean = false; var downbumping:boolean = false; var leftbumppoint:array = new array(leftbumppoint1, leftbumppoint2, leftbumppoint3); var leftbumppoint1:point = new point(-30, -55); var leftbumppoint2:point = new point(-30, 0); var leftbumppoint3:point = new point(-30, -116.5); var rightbumppoint:point = new point(30, -55); var upbumppoint:point = new point(0, -120); var downbumppoint:point = new point(0, 0); var scrollx:number = 0; var scrolly:number = 500; var xspeed:number = 0; var yspeed:number = 0; var speedconstant:number = 4; var frictionconstant:number = 0.9; var gravityconstant:number = 1.8; var jumpconstant:number = -35; var maxspeedconstant:number = 18; var doublejumpready:boolean = false; var upreleasedinair:boolean = false; var keycollected:boolean = false; var dooropen:boolean = false; var currentlevel:int = 1; var pscore:int = 0; var lives:int = 10; var animationstate:string = "idle"; var bulletlist:array = new array(); var enemylist:array = new array(); var bumperlist:array = new array(); keystat.visible = false; keystat.stop(); doorstat.x = 6.66; stage.addeventlistener(keyboardevent.key_down, keydownhandler); stage.addeventlistener(keyboardevent.key_up, keyuphandler); stage.addeventlistener(event.enter_frame, loop); addenemiestolevel(); lifetxt.text=string(lives); scoretxt.text=string(pscore); function addenemiestolevel():void { if(currentlevel == 1){ addenemy(620, -115); addbumper(500, -115); addbumper(740, -115); addenemy(900, -490); addbumper(380, -490); addbumper(1020, -490); addenemy(2005, -115); addbumper(2125, -115); addbumper(1885, -115); addenemy(1225, -875); addbumper(1345, -875); addbumper(1105, -875); } if(currentlevel == 2){ addenemy(2005, -115); addbumper(2125, -115); addbumper(1885, -115); } } function loop(e:event):void{ if(back.collisions.hittestpoint(player.x + leftbumppoint.x, player.y + leftbumppoint.y, true)){ leftbumping = true; } else { leftbumping = false; } if(back.collisions.hittestpoint(player.x + rightbumppoint.x, player.y + rightbumppoint.y, true)){ rightbumping = true; } else { rightbumping = false; } if(back.collisions.hittestpoint(player.x + upbumppoint.x, player.y + upbumppoint.y, true)){ upbumping = true; } else { upbumping = false; } if(back.collisions.hittestpoint(player.x + downbumppoint.x, player.y + downbumppoint.y, true)){ downbumping = true; } else { downbumping = false; } if(leftpressed){ xspeed -= speedconstant; player.scalex = -1; } else if(rightpressed){ xspeed += speedconstant; player.scalex = 1; } if(leftbumping){ if(xspeed < 0){ xspeed *= -0.5; } } if(rightbumping){ if(xspeed > 0){ xspeed *= -0.5; } } if(upbumping){ if(yspeed < 0){ yspeed *= -0.5; } } if(downbumping){ if(yspeed > 0){ yspeed = 0; } if(uppressed){ yspeed = jumpconstant; } //double jump if(upreleasedinair == true){ upreleasedinair = false; } if(doublejumpready == false){ doublejumpready = true; } } else { yspeed += gravityconstant; //double jump if(uppressed == false && upreleasedinair == false){ upreleasedinair = true; } if(doublejumpready && upreleasedinair){ if(uppressed){ doublejumpready = false; yspeed = jumpconstant; } } } if(keycollected == false){ if(player.hittestobject(back.other.doorkey)){ back.other.doorkey.visible = false; keycollected = true; keystat.visible = true; doorstat.x = 41.95; trace("key collected"); } } if(dooropen == false){ if(keycollected == true){ if(player.hittestobject(back.other.lockeddoor)){ back.other.lockeddoor.gotoandstop(2); dooropen = true; keystat.visible = false; doorstat.x = 6.66; doorstat.gotoandstop(2); trace("door open"); } } } if(xspeed > maxspeedconstant){ //moving right xspeed = maxspeedconstant; } else if(xspeed < (maxspeedconstant * -1)){ //moving left xspeed = (maxspeedconstant * -1); } xspeed *= frictionconstant; yspeed *= frictionconstant; if(math.abs(xspeed) < 0.5){ xspeed = 0; } scrollx -= xspeed; scrolly -= yspeed; back.x = scrollx; back.y = scrolly; sky.x = scrollx * 0.2; sky.y = scrolly * 0.2; if( ( leftpressed || rightpressed || xspeed > speedconstant || xspeed < speedconstant *-1 ) && downbumping){ animationstate = "running"; } else if(downbumping){ animationstate = "idle"; }/** else if(doublejumpready && upreleasedinair){ if(uppressed){ animationstate = "double"; } }**/ else { animationstate = "jumping"; } if(player.currentlabel != animationstate){ player.gotoandstop(animationstate); } if (enemylist.length > 0) { for (var i:int = 0; < enemylist.length; i++) { if (bulletlist.length > 0) { for (var j:int = 0; j < bulletlist.length; j++) { if ( enemylist[i].hittestobject(bulletlist[j]) ) { trace("killed enemy"); pscore += 10; scoretxt.text=string(pscore); enemylist[i].removeself(); bulletlist[j].removeself(); } } } } } if (enemylist.length > 0){ for (var k:int = 0; k < enemylist.length; k++){ if (bumperlist.length > 0){ for (var h:int = 0; h < bumperlist.length; h++){ if ( enemylist[k].hittestobject(bumperlist[h]) ){ enemylist[k].changedirection(); } } } } } if (enemylist.length > 0){ for (var m:int = 0; m < enemylist.length; m++){ if ( enemylist[m].hittestobject(player) ){ trace("player got killed"); scrollx = 0; scrolly = 500; lives -= 1; lifetxt.text=string(lives); } } } if (lives == 0){ stage.removeeventlistener(keyboardevent.key_down, keydownhandler); stage.removeeventlistener(keyboardevent.key_up, keyuphandler); stage.removeeventlistener(event.enter_frame, loop); gotoandstop("gameover"); if(yourlevel <= currentlevel){ myso.data.mylevel = yourlevel; myso.flush(); }else{ trace("you beat level"); } } } var maxlevel:int = back.collisions.totalframes; function nextlevel():void{ currentlevel++; doorstat.gotoandstop(1); if(yourlevel <= currentlevel){ myso.data.mylevel = yourlevel; myso.flush(); }else{ trace("you beat level"); } trace("next level: " + currentlevel); gotonextlevel(); if(currentlevel > maxlevel){ stage.removeeventlistener(keyboardevent.key_down, keydownhandler); stage.removeeventlistener(keyboardevent.key_up, keyuphandler); stage.removeeventlistener(event.enter_frame, loop); gotoandstop("gameover"); } } function removeenemy():void { /**if(currentlevel == 1){ }**/ } function gotonextlevel():void{ removeenemy(); back.other.gotoandstop(currentlevel); back.visuals.gotoandstop(currentlevel); back.collisions.gotoandstop(currentlevel); scrollx = 0; scrolly = 500; keycollected = false; back.other.doorkey.visible = true; dooropen = false; back.other.lockeddoor.gotoandstop(1); } function keydownhandler(e:keyboardevent):void{ if(e.keycode == keyboard.left){ leftpressed = true; } else if(e.keycode == keyboard.right){ rightpressed = true; } else if(e.keycode == keyboard.up){ uppressed = true; } else if(e.keycode == keyboard.down){ downpressed = true; if(dooropen && player.hittestobject(back.other.lockeddoor)){ nextlevel(); } } } function keyuphandler(e:keyboardevent):void{ if(e.keycode == keyboard.left){ leftpressed = false; } else if(e.keycode == keyboard.right){ rightpressed = false; } else if(e.keycode == keyboard.up){ uppressed = false; } else if(e.keycode == keyboard.down){ downpressed = false; } if(e.keycode == keyboard.space){ firebullet(); } } function firebullet():void { var playerdirection:string; if(player.scalex < 0){ playerdirection = "left"; } else if(player.scalex > 0){ playerdirection = "right"; } var bullet:bullet = new bullet(player.x - scrollx, player.y - scrolly, playerdirection, xspeed); back.addchild(bullet); bullet.addeventlistener(event.removed, bulletremoved); bulletlist.push(bullet); } function bulletremoved(e:event):void { e.currenttarget.removeeventlistener(event.removed, bulletremoved); bulletlist.splice(bulletlist.indexof(e.currenttarget), 1); } function addenemy(xlocation:int, ylocation:int):void { var enemy:enemy = new enemy(xlocation, ylocation); back.addchild(enemy); enemy.addeventlistener(event.removed, enemyremoved); enemylist.push(enemy); } function addbumper(xlocation:int, ylocation:int):void { var bumper:bumper = new bumper(xlocation, ylocation); back.addchild(bumper); bumper.visible = false; bumperlist.push(bumper); } function enemyremoved(e:event):void { e.currenttarget.removeeventlistener(event.removed, enemyremoved); enemylist.splice(enemylist.indexof(e.currenttarget), 1); }
where's code removing enemies when level ends?
More discussions in ActionScript 3
adobe
Comments
Post a Comment