1

大家好,我在玩一个简单的 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;
        }
    }
}

}

4

1 回答 1

2

您的laser对象已附加到this,但出于某种最奇怪的原因,您laser.removeChild()本质上是要求laser将其从自身中删除。你已经省略parent了那里。

laser.parent.removeChild(laser);

此外,如果您的激光将同时到达舞台边缘并gameObject在同一帧上击中 a ,则存在一个缺陷,尝试laser连续两次删除时会遇到 1009 错误,第一次调用 null laser.parent,第二次调用将调用方法空引用。你也必须解决这个问题,可能if (laser.parent)就像你对对象所做的那样。

更新:您还从错误的对象中删除了事件侦听器,您将其连接到激光,并从中删除this不是激光。你应该做:

laser.removeEventListener(Event.ENTER_FRAME, moveLaser);
于 2013-04-02T03:27:19.763 回答