首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AS3阶段=空?

AS3阶段=空?
EN

Stack Overflow用户
提问于 2012-11-19 04:48:40
回答 3查看 3.9K关注 0票数 3

我刚刚尝试实现一个菜单系统,我在我正在开发的游戏上看到了一个教程,一切都很顺利,直到现在我遇到了阶段设置为null的问题,我不知道如何停止它。不断中断它的行在AvoiderGame.as文件中,它是stage.addEventListener(Event.ADDED_TO_STAGE, init);,它不断返回以下错误

代码语言:javascript
运行
复制
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中启动。

代码语言:javascript
运行
复制
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,直到用户单击play - inside mainMenu.as是以下代码

代码语言:javascript
运行
复制
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,然后用一个我不知道如何修复的错误返回给我-有人能建议我如何修复它吗?

代码语言:javascript
运行
复制
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;
    }
}
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-19 04:52:18

代码语言:javascript
运行
复制
public function AvoiderGame(callBack)
{
    stage.addEventListener(Event.ADDED_TO_STAGE, init);
    theCallBackFunction = callBack;
}

应该是这样的

代码语言:javascript
运行
复制
public function AvoiderGame(callBack)
{
    this.addEventListener(Event.ADDED_TO_STAGE, init);
    theCallBackFunction = callBack;
}

在调用init函数时,不要忘记删除事件侦听器。

票数 4
EN

Stack Overflow用户

发布于 2012-11-19 04:51:03

如果未将显示对象添加到舞台或显示列表中已有的其它显示对象,则stage为null。当设置了stage并且可用时,将调度Event.ADDED_TO_STAGE。

票数 2
EN

Stack Overflow用户

发布于 2012-11-19 04:52:33

当您创建AvoiderGame对象时,它将触发构造函数。此时,该对象不会添加到舞台或显示列表中的任何对象,因此它将不会有任何对舞台的引用。

可以将侦听器添加到ADDED_TO_STAGE事件,也可以在手动将其添加到舞台之前调用自定义init方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13444408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档