1

使用 flash as3 创建菜单时遇到问题。你们能帮我怎么办...?像这样 :

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at DocumentClass/menuScreen()
at DocumentClass/mouseBack()

如何正确的代码来修复下面的代码:

package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.StageDisplayState;

public class DocumentClass extends MovieClip {
public var backbutton:BackButton = new BackButton();
    public function DocumentClass() {
        menuScreen();
        stage.addEventListener(Event.ENTER_FRAME, masukGame);

    }
    public function masukGame(e:Event):void{
        stage.displayState = StageDisplayState.FULL_SCREEN;
    }
    public function menuScreen():void{

        game.menuscreen.mb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick1);
        game.menuscreen.pb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick2);
        game.menuscreen.mlb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick3);
        game.menuscreen.kb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick4);


    }
    public function backMenu():void{

        backbutton.buttonMode = true;
        backbutton.addEventListener(MouseEvent.MOUSE_DOWN, mouseBack);
        backbutton.x = 450;
        backbutton.y = 400;
        // Dan akhirnya letakan button ke stage;
        addChild(backbutton);
        backbutton.visible=true;

    }

    public function mouseClick1(event:MouseEvent):void {
        game.gotoAndStop("MainGame");
        backMenu();


    }
    public function mouseClick2(event:MouseEvent):void {
        game.gotoAndStop("Petunjuk");
        backMenu();
    }
    public function mouseClick3(event:MouseEvent):void {
        game.gotoAndStop("Malaria");
        backMenu();
    }
    public function mouseClick4(event:MouseEvent):void {
        game.gotoAndStop("Keluar");
        backMenu();
    }
    public function mouseBack(event:MouseEvent):void {

        menuScreen();


    }   


    }
}

这是我的文件,在这里下载:https ://www.dropbox.com/s/jo7bcwnlfj7qjnh/My%20Game.rar

请帮助我,谢谢各位...

4

2 回答 2

1

您的问题是Null Pointer Exception
这意味着您正在尝试访问空对象内的变量。

您提供的 StackTrace 指导我们解决问题。
跟踪中的最后一个位置是:

DocumentClass/menuScreen()

所以,你的问题menuScreen()DocumentClass课堂上。

public function menuScreen():void
{
    game.menuscreen.mb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick1);
    game.menuscreen.pb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick2);
    game.menuscreen.mlb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick3);
    game.menuscreen.kb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick4);
}

这里我们尝试访问:

  • game
  • game.menuscreen
  • game.menuscreen.mb
  • game.menuscreen.pb
  • game.menuscreen.mlb
  • game.menuscreen.kb

其中之一为空。


编辑 1

好的,我尝试了您的代码.fla,发现问题出在game.menuscreen调用上。
更改屏幕后,您将其menuscreen从舞台上移除。

当您返回并尝试访问它时,它不再存在,因此,您会得到一个 NPE(空指针异常)。


编辑 2

您可以使用不同的方法解决问题,但最简单(也是最不灵活)的方法是保留对您的menuscreen某个地方的引用,您可以稍后再获取它。

例如:您可以通过这种方式将其保留在DocumentClass课堂上:

public class DocumentClass extends MovieClip
{
    public var backbutton:BackButton = new BackButton();
    public var gameMenuScreen:MovieClip;

    public function DocumentClass()
    {
        gameMenuScreen = game.menuscreen;
        menuScreen();
        stage.addEventListener(Event.ENTER_FRAME, masukGame);
    }

    // ... rest of the class
}

然后你可以直接从 中访问它DocumentClass,这样:

public function menuScreen():void
{
    // It was "game.menuscreen"
    // Now it is "gameMenuScreen"

    gameMenuScreen.mb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick1);
    gameMenuScreen.pb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick2);
    gameMenuScreen.mlb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick3);
    gameMenuScreen.kb.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick4);
}
于 2013-05-21T16:23:33.133 回答
0

解决办法是把flash换成另一种版本。我尝试了它,问题就消失了。

于 2014-07-17T00:00:53.797 回答