Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 flash as3中出现错误#1009_Actionscript 3_Flash_Flash Cs6 - Fatal编程技术网

Actionscript 3 flash as3中出现错误#1009

Actionscript 3 flash as3中出现错误#1009,actionscript-3,flash,flash-cs6,Actionscript 3,Flash,Flash Cs6,我是flash和as3编程新手,这是我的第一个项目。我发现我的项目有这样的错误 TypeError:错误#1009:无法访问空对象引用的属性或方法。 在src.char::敌方/Remove()处 在src.screen::Gameplay/Collision()中 在src.screen::Gameplay/Routine()中 我认为出现错误是因为游戏中没有函数Remove(),但我不确定这是真的。敌人来了 import flash.display.MovieClip; import fla

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

TypeError:错误#1009:无法访问空对象引用的属性或方法。 在src.char::敌方/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);
    }

}
这是游戏性

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

}
public类游戏扩展了MovieClip{
专用var定时器:定时器=新定时器(500);
私人玩家:玩家;
公共功能游戏性(){
addEventListener(Event.ADDED_TO_STAGE,InitKeyboard);
addEventListener(Event.ENTER_FRAME,例程);
gameplayBack.addEventListener(MouseEvent.CLICK,GoToMap);
timer.addEventListener(TimerEvent.timer,OnTick);
timer.start();
InitPlayer();
InitLifePoint();
}
私有函数InitLifePoint():void{
lifePoint.gotoAndStop(1);
}
专用函数例程(e:事件):无效{
碰撞();
}
私有函数冲突():void{
for(变量i:int=0;i<0.length;i++){
if(player.hitTestObject(敌人[i])){
PlayerHit();
敌人[i].Remove();
返回;
}否则{
对于(变量j:int=0;j
您的问题是敌人内部的NPE(空指针异常/错误)。Remove()方法:

您的
timer
属性为null(查看您的代码,我怀疑这一点),或者
parent
属性为null

MovieClip
中,如果将
MovieClip
添加到
parent
属性中,则该属性将填充
DisplayObject
,如果未添加,则该属性为
null

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

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

if(parent != null)
{
    parent.removeChild(this);
}
for (var i:int = 0; i < enemies.length; i++) {
            if (~) {
                ...
                enemies[i].Remove();
                ...
            } else {
                for (~) {
                    if (~) {
                        ...
                        enemies[i].Remove();
                        ...
                    }
                }
            }
        }
注意
这可能会解决您的NPE问题,但不会解决导致问题的原因,这可能会导致您出现越来越多无法解释的错误。

仔细检查您的逻辑,以确保您正在删除以前添加的
MovieClip
,或者您没有多次删除它。

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

if(parent != null)
{
    parent.removeChild(this);
}
for (var i:int = 0; i < enemies.length; i++) {
            if (~) {
                ...
                enemies[i].Remove();
                ...
            } else {
                for (~) {
                    if (~) {
                        ...
                        enemies[i].Remove();
                        ...
                    }
                }
            }
        }
for(变量i:int=0;i<0.length;i++){
if(~){
...
敌人[i].Remove();
...
}否则{
对于(~){
if(~){
...
敌人[i].Remove();
...
}
}
}
}
显然,在第二个for循环中,您可以很容易地引用相同的敌人对象

在调用Remove函数之后出现问题,因为通过执行
parent.removeChild(这)删除对象对其父对象的唯一引用

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

  • 手动保留对类中父对象的引用
  • 在类中保留一个状态变量,以检查它是否是显示列表的一部分
  • 移动
    敌人[i].Remove()编码到最外层的循环中
  • 如果可能,回收该对象。尤其是当你刚刚 移动(x,y)电影剪辑

我不明白你的意思,你能说得更清楚些吗?我得到了另一个错误,上面写着“ReferenceError:error#1069:Property Remove在src.char.敌手上找不到,并且没有默认值。在src.screen::Gameplay/collation()在src.screen::Gameplay/Routine()”可能的答案是什么