-1

我是 Flash 和 as3 编程的新手,这是我的第一个项目。我在我的项目中发现了这样的错误

TypeError:错误 #1009:无法访问空对象引用的属性或方法。在 src.char::Enemy/Remove() 在 src.screen::Gameplay/Collision() 在 src.screen::Gameplay/Routine()

我认为发生错误是因为游戏中没有函数Remove(),但我不确定这是不是真的。这是敌人.as

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;

public class Enemy extends MovieClip {

    private var timer:Timer = new Timer(25);

    public function Enemy(xPos:Number, yPos:Number) {
        x = xPos;
        y = yPos;
        timer.addEventListener(TimerEvent.TIMER, MoveDown);
        timer.start();
    }

    private function MoveDown(e:TimerEvent):void {
        y += 3;
        if (y>400) {
            Remove();
        }
    }

    public function Remove():void {
        timer.stop();
        timer.removeEventListener(TimerEvent.TIMER, MoveDown);
        parent.removeChild(this);
    }

}

这是gameplay.as

    public class Gameplay extends MovieClip {

    private var timer:Timer = new Timer(500);
    private var player:Player;

    public function Gameplay() {
        addEventListener(Event.ADDED_TO_STAGE, InitKeyboard);
        addEventListener(Event.ENTER_FRAME, Routine);
        gameplayBack.addEventListener(MouseEvent.CLICK, GoToMap);
        timer.addEventListener(TimerEvent.TIMER, OnTick);
        timer.start();
        InitPlayer();
        InitLifePoint();
    }

    private function InitLifePoint():void {
        lifePoint.gotoAndStop(1);           
    }

    private function Routine(e:Event):void {
        Collision();
    }

    private function Collision():void {
        for (var i:int = 0; i < enemies.length; i++ ) {
            if (player.hitTestObject(enemies[i])) {
                PlayerHit();
                enemies[i].Remove();
                return;
            }else {
                for (var j:int = 0; j < bullets.length; j++ ) {
                    if (bullets[j].hitTestObject(enemies[i])) {
                        layerParticle.addChild(new Blast(bullets[j].x, bullets[j].y));
                        layerParticle.addChild(new Smoke(bullets[j].x, bullets[j].y));
                        bullets[j].Remove();
                        enemies[i].Remove();
                        scorePlay.text = int(scorePlay.text) + 10 + "";
                        trace(scorePlay.text);
                        return;
                    }
                }
            }
        }
    }


    private var life:int = 1000;
    private var currentLife:int = 1000;
    private function PlayerHit():void {
        currentLife -= 100;
        if (currentLife <= 0) {
            lifePoint.gotoAndStop(100);
            GameOver();
        }else {
            lifePoint.gotoAndStop(100 - currentLife / life * 100);
        }
    }

    private var result:Result = new Result();
    private function GameOver():void {
        result.youWin.alpha = 0;
        result.ok.addEventListener(MouseEvent.CLICK, GoToMap);
        result.x = 0;
        result.y = 0;
        addChild(result);
    }

    private function InitKeyboard(e:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, InitKeyboard);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
    }

    private function KeyDown(e:KeyboardEvent):void {
        switch(e.keyCode) {
            case Keyboard.LEFT: MoveLeft(); break;
            case Keyboard.RIGHT: MoveRight(); break;
            case Keyboard.SPACE: Fire(); break;
        }
    }

    private var bullets:Array = new Array();
    private function Fire():void {
        var bullet:Bullet = new Bullet(player.x, player.y);
        bullet.scaleX = 0.25;
        bullet.scaleY = 0.25;
        bullet.addEventListener(Event.REMOVED_FROM_STAGE, RemoveBulletArray);
        layerParticle.addChild(bullet);
        bullets.push(bullet);
    }

    private function RemoveBulletArray(e:Event):void {
        removeEventListener(Event.REMOVED_FROM_STAGE, RemoveBulletArray);
        var index:int = bullets.indexOf(Bullet(e.currentTarget), 0);
        bullets.splice(index, 1);
    }

    private function MoveRight():void {
        if (player.x < 550) {
            player.x += 5;
        }
    }

    private function MoveLeft():void {
        if (player.x > 0) {
            player.x -= 5;
        }
    }

    private function InitPlayer():void {
        player = new Player(550 * 0.5, 350);
        layerChar.addChild(player);
    }

    private function OnTick(e:TimerEvent):void {
        RandomEnemy();
    }

    private var enemies:Array = new Array();
    private function RandomEnemy():void {
        var enemy:Enemy = new Enemy(Math.random() * 550, 0);
        enemy.addEventListener(Event.REMOVED_FROM_STAGE, RemoveFromArray);
        layerChar.addChild(enemy);
        enemies.push(enemy);
    }

    private var remaining:int = 10;
    private function RemoveFromArray(e:Event):void {
        removeEventListener(Event.REMOVED_FROM_STAGE, RemoveFromArray);
        var index:int = enemies.indexOf(Enemy(e.currentTarget), 0);
        enemies.slice(index, 1);
        remaining--;
        if (remaining == 0) GameWin();
    }

    private function GameWin():void {
        result.youLose.alpha = 0;
        result.score.text = scorePlay.text;
        result.ok.addEventListener(MouseEvent.CLICK, GoToMap);
        result.x = 0;
        result.y = 0;
        addChild(result);
    }

    private function GoToMap(e:MouseEvent):void {
        dispatchEvent(new ScreenEvent(ScreenEvent.MAP));
    }

}
4

2 回答 2

1

您的问题是 Enemy.Remove() 方法中的 NPE(空指针异常/错误):

public function Remove():void {
    timer.stop();
    timer.removeEventListener(TimerEvent.TIMER, MoveDown);
    parent.removeChild(this);
}

您的timer财产为空(我怀疑,查看您的代码)或财产为空parent

在 a中,如果添加了您MovieClipparent属性,则使用 a 填充,如果没有,则此属性为。DisplayObjectMovieClipnull

MovieClip您的问题可能是您多次(从其父级)删除它,或者试图在不先添加它的情况下删除它。

要确保这是问题所在,请先添加一条if语句来检查parent属性,如下所示:

if(parent != null)
{
    parent.removeChild(this);
}

注意
这可能会解决您的 NPE 问题,但不会解决导致它的原因,这可能会导致您陷入越来越多的莫名其妙的错误。
仔细检查您的逻辑以确保您正在删除以前添加的MovieClip,或者您没有多次删除它。

于 2013-01-26T17:29:54.087 回答
0

如果您像这样观察,您是否注意到碰撞功能中的缺陷:

for (var i:int = 0; i < enemies.length; i++) {
            if (~) {
                ...
                enemies[i].Remove();
                ...
            } else {
                for (~) {
                    if (~) {
                        ...
                        enemies[i].Remove();
                        ...
                    }
                }
            }
        }

显然,在第二个for循环中,您可以轻松引用同一个 Enemy 对象。

问题出现在您调用Remove函数之后,因为通过这样做, parent.removeChild(this);您删除了对象对其父对象的唯一引用。

您可以尝试执行以下操作之一:

  • 手动保留对类中父对象的引用。
  • 在类中保留一个状态变量以检查它是否是显示列表的一部分。
  • enemies[i].Remove();代码移动到最外层循环。
  • 如果可能,回收该物品。特别是,当您只是移动 (x,y) 影片剪辑时。
于 2013-01-26T18:21:11.530 回答