flash as3中出现错误#1009

时间:2013-01-26 15:38:31

标签: actionscript-3 flash flash-cs6

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

  

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

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

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));
    }

}

2 个答案:

答案 0 :(得分:1)

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

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

您的timer属性为空(我怀疑,查看您的代码)或parent属性。

MovieClip parent属性中,如果添加了DisplayObject,则MovieClip会填充null,如果不是,则此属性为MovieClip

您的问题可能是您要多次从if移除parent,或者尝试将其删除而不先添加它。

要确保这是问题,请首先添加if(parent != null) { parent.removeChild(this); } 语句以检查MovieClip属性,如下所示:

{{1}}

注意
这可能会解决您的NPE问题,但无法解决导致它的问题,这可能会导致您遇到越来越多的莫名其妙的错误。
仔细检查您的逻辑,确保删除之前添加的{{1}},或者您不会删除多次。

答案 1 :(得分:0)

如果你这样观察,你会注意到碰撞功能中的缺陷:

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

显然,在第二个 for 循环中,您可以轻松引用相同的敌人对象。

问题出现在您调用删除函数之后,因为通过执行parent.removeChild(this);您将对象的唯一引用移除到它的父对象。

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

  • 手动保持对类中父对象的引用。
  • 在类中保留一个状态变量,以检查它是否是显示列表的一部分。
  • enemies[i].Remove();代码移至最外圈。
  • 如果可能,请回收该对象。特别是,当你是公正的时候 移动(x,y)动画片段。