AS3阶段= NULL?(AS3 stage = null?)

2019-07-03 22:50发布

我只是想实现我看到了一个教程在游戏的菜单系统,我的工作,这是一帆风顺,直到我现在遇到的阶段出现问题被设置为空,我不知道如何阻止它。 该不断打破它的线是在AvoiderGame.as文件,并且是stage.addEventListener(Event.ADDED_TO_STAGE, init); 它一直返回以下错误

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at AvoiderGame()[M:\Users\Andy\Documents\Programming For Fun\Flash\Tutorial - Avoider Game\Classes\AvoiderGame.as:29]

at States/changeState()[M:\Users\Andy\Documents\Programming For Fun\Flash\Tutorial - Avoider Game\States.as:26]

at mainMenu/brnP_Button()[M:\Users\Andy\Documents\Programming For Fun\Flash\Tutorial - Avoider Game\Classes\mainMenu.as:34]

目前,我已设定我的比赛方式是使其在States.as包含以下代码开始。

package
{
import flash.display.*;
import flash.system.fscommand;

public class States extends MovieClip
{
    public function States()
    {
        changeState(null, "menu");
    }
    public function changeState (currentState, nextState)
    {
        if (currentState != null)
        {
            removeChild(currentState);
        }
        switch(nextState)
        {
            case "menu": var mm:mainMenu = new mainMenu(changeState); 
                         addChild(mm);
            break;
            case "game": var g:AvoiderGame = new AvoiderGame(changeState);
                         addChild(g);
            break;
            case "exit": fscommand("quit");
            break;
        }
    }

}

}

从那里,它会进入mainMenu.as,直到用户点击播放 - 内mainMenu.as是下面的代码

package
{
import flash.display.*;
import flash.events.*;

public class mainMenu extends MovieClip
{
var theCallBackFunction:Function;

public function mainMenu(callBack)
{
    var Background:gameBackground;
    Background = new gameBackground();
    addChild(Background);

    var btnPlay:mmPlay = new mmPlay();
    btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, brnP_Button);
    btnPlay.x = width/2;
    btnPlay.y = height/2 - btnPlay.height/2;
    addChild(btnPlay);

    var btnExit:mmExit = new mmExit();
    btnExit.addEventListener(MouseEvent.MOUSE_DOWN, brnE_Button);
    btnExit.x = width/2;
    btnExit.y = height/2 - btnExit.height/2;
    btnExit.y += btnExit.height + 4;
    addChild(btnExit);

    theCallBackFunction = callBack;
}
public function brnP_Button(e:MouseEvent)
{
    theCallBackFunction(this, "game");
    return;
}

public function brnE_Button(e:MouseEvent)
{
    theCallBackFunction(this, "exit");
    return;
}

}
}

现在,这是它出错 - 它进入AvoiderGame.as,然后踢我回来了,我不知道如何解决错误 - 谁能告诉我如何解决这个问题?

package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import com.freeactionscript.CollisionTest;
import flash.display.Stage;

public class AvoiderGame extends MovieClip
{
    var theCallBackFunction:Function;

    public static var enemyArray:Array;
    public var enemy:Enemy
    public var Background:gameBackground;

    public var avatar:Avatar;
    public var gameTimer:Timer;

    private var _collisionTest:CollisionTest;
    private var numStars:int = 80;

    private var fireTimer:Timer; //causes delay between fires
    private var canFire:Boolean = true; //can you fire a laser

    public function AvoiderGame(callBack)
    {
        stage.addEventListener(Event.ADDED_TO_STAGE, init);
        theCallBackFunction = callBack;
    }

    private function init(e:Event):void
    {
        Background = new gameBackground();
        addChild(Background);

        enemyArray = new Array();
        var enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 2, stage);
        enemyArray.push(enemy);
        addChild(enemy);

        avatar = new Avatar(stage);
        addChild(avatar);

        avatar.x = stage.stageWidth / 2;
        avatar.y = stage.stageHeight / 2;

        for (var i:int = 0; i < numStars; i++)
        {
            stage.addChildAt(new Star(stage), 1);
        }

        _collisionTest = new CollisionTest();

        gameTimer = new Timer(25);
        gameTimer.addEventListener(TimerEvent.TIMER, onTick);
        gameTimer.start();

        fireTimer = new Timer(300, 1);
        fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
        fireTimer.start();
    }

    public function onTick(timerEvent:TimerEvent):void 
    {
        if (Math.random() < 0.1)
        {
            trace('array length: ', AvoiderGame.enemyArray.length);
            enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 28, stage);
            enemyArray.push(enemy);
            addChild(enemy);
        }

        avatar.UpdateAvatar(canFire);
        if (canFire == true)
        {
            canFire = false;
            fireTimer.start();
        }
        avatar.StayOnScreen();

        for each (var enemy:Enemy in enemyArray)
        {
            enemy.moveDown();
            enemy.StayOnScreen();
            if (_collisionTest.complex(enemy, avatar)) 
            {
                gameTimer.stop();
            }
        }
    }
    private function fireTimerHandler(e:TimerEvent) : void
    {
        //timer ran, we can fire again.
        canFire = true;
    }
}
}

Answer 1:

public function AvoiderGame(callBack)
{
    stage.addEventListener(Event.ADDED_TO_STAGE, init);
    theCallBackFunction = callBack;
}

应该是这样的

public function AvoiderGame(callBack)
{
    this.addEventListener(Event.ADDED_TO_STAGE, init);
    theCallBackFunction = callBack;
}

不要忘记删除事件监听器当初始化函数被调用。



Answer 2:

当显示对象未添加到舞台或其他显示对象已经在显示列表中,阶段为空。 Event.ADDED_TO_STAGE将分赴,当舞台设置和使用。



Answer 3:

当您创建AvoiderGame对象,它西港岛线火的构造。 此时,对象不被添加到该阶段或在显示列表中的任何对象,因此它不会有阶段的任何参考。

您可以将监听器添加到ADDED_TO_STAGE事件或调用自定义init手动将其添加到舞台前的方法。



文章来源: AS3 stage = null?