Hello Everyone我在使用简单的Flash游戏时遇到了麻烦。到目前为止,一旦击中物体,就无法移除射弹。
通过网站后发现要添加.parent.removeChild(wtv);删除对象。当使用我的玩家触发的对象击中一个对象时,两个都被移除但是当另一个被射击在同一个位置时,被射击的对象击中一个刚被移除的不可见对象,然后在该位置被移除而不是继续进行直到阶段结束或另一个对象被击中。这似乎只有在删除被触发的对象时才会发生。
这是代码,我相信问题出在moveLaser功能中,如果需要更多信息请告诉我,任何建议都会很棒,谢谢。
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display3D.IndexBuffer3D;
public class Level_1 extends MovieClip{
private var player:SpaceShip;
private var objects:Array;
private var probMoveObj:int;
private var bgStars:Level1_stars;
public function Level_1(){
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
public function onAddedToStage(event:Event):void{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
probMoveObj = 1;
bgStars = new Level1_stars();
player = new SpaceShip();
objects = new Array(50);
bgStars.vx = 50;
addChild(bgStars);
for(var i:int = 0; i < objects.length; i++){
objects[i] = new GameObject();
addGameObjects(objects[i]);
}
player.x = 100;
player.y = stage.stageHeight - 250;
addChild(player);
bgStars.addEventListener(Event.ENTER_FRAME, moveBackground);
player.addEventListener(Event.ENTER_FRAME, movePlayer);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkFire);
}
public function movePlayer(event:Event):void{
/*for(var i:int = 0; i < objects.length; i++){
if(objects[i].hitTestObject(player.hit)){
objects[i].removeEventListener(Event.ENTER_FRAME, moveObject);
trace("HIT");
}
}*/
}
public function checkFire(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.SPACE){
trace("SPACE");
var laser:Ship_laser = new Ship_laser();
var laser2:Ship_laser = new Ship_laser();
laser.x = player.x + 30;
laser2.x = player.x + 30;
laser.y = player.y - 30;
laser2.y = player.y + 30;
laser.addEventListener(Event.ENTER_FRAME, moveLaser);
laser2.addEventListener(Event.ENTER_FRAME, moveLaser);
addChild(laser);
addChild(laser2);
}
}
public function checkKey(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.UP){
if((player.y - (player.height / 2)) >= 0){
player.y -= 10;
trace("UP");
}
}
else if(event.keyCode == Keyboard.DOWN){
if((player.y +(player.height / 2)) <= stage.stageHeight){
player.y += 10;
trace("down");
}
}
else if(event.keyCode == Keyboard.LEFT){
if(player.x - (player.width / 2) >= 0){
player.x -= 10;
trace("LEFT");
}
}
else if(event.keyCode == Keyboard.RIGHT){
if(player.x + (player.width / 2) <= stage.stageWidth){
player.x += 10;
trace("RIGHT");
}
}
}
public function addGameObjects(gameObj:GameObject):void{
// position the object randomly, but to the right of the player
gameObj.x = stage.stageWidth + (Math.ceil(Math.random() * (4000 + gameObj.width/2)));
gameObj.y = Math.ceil(Math.random() * (stage.stageHeight - 100 - gameObj.height/2));
// add object to the stage
addChild(gameObj);
// set the velocity of the object
gameObj.vx = -Math.ceil((Math.random() + 1) * 2);
// move the object each frame
gameObj.addEventListener(Event.ENTER_FRAME, moveObject);
}
private function moveObject(event:Event):void
{
var gameObj:GameObject = event.target as GameObject;
gameObj.x += gameObj.vx;
}
private function moveBackground(event:Event):void{
if(bgStars.x < ((bgStars.width / 2 + 100) * -1)){
bgStars.x = 0;
}
else{
bgStars.x -= bgStars.vx;
}
}
private function moveLaser(event:Event):void{
var laser:Ship_laser = event.target as Ship_laser;
for(var i:int = 0; i < objects.length; i++){
if(objects[i].hitTestObject(laser.hit)){
objects[i].removeEventListener(Event.ENTER_FRAME, moveObject);
if(objects[i].parent){
objects[i].parent.removeChild(objects[i]);
}
removeEventListener(Event.ENTER_FRAME, moveLaser);
if(laser.parent){
laser.parent.removeChild(laser);
}
}
}
if(laser.x > stage.stageWidth){
trace("LASER REMOVED");
removeEventListener(Event.ENTER_FRAME, moveLaser);
if(laser.parent){
laser.parent.removeChild(laser);
}
}
else{
laser.x += 20;
}
}
}
}
答案 0 :(得分:1)
您的laser
对象已附加到this
,但出于某些最奇怪的原因,请致电laser.removeChild()
,基本上要求laser
将其自身移除。你在那里省略了parent
。
laser.parent.removeChild(laser);
此外,如果您的激光器同时到达舞台边缘并在同一帧上点击gameObject
,则会出现一个缺陷,如果尝试删除laser
两次,则会出现1009错误一行,首先调用null laser.parent
,第二次调用将调用null引用的方法。您也必须像对待对象一样通过if (laser.parent)
解决此问题。
更新:您还从错误的对象中删除事件侦听器,将其附加到激光器,然后从不是激光器的this
中删除。你应该这样做:
laser.removeEventListener(Event.ENTER_FRAME, moveLaser);