AS3不允许我从其他.as文件访问类

时间:2014-03-20 09:58:08

标签: actionscript-3 flash class packages flash-cs6

我有MainClass.as作为我的文档类。然后我创建了菜单。现在我希望这些菜单位于不同的文件中(包... idk)。

这是我来自screens.as的新游戏按钮:

    private function newGameButton(Event:MouseEvent):void
    {
        var levels:Levels = new Levels();

        stage.focus = stage;
        removeChild(CurrentScreen);
        levels.gotoLevelOne();
    }

这是Levels.as:

package  {

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.LoaderInfo;
import flash.events.Event;

public class Levels extends MovieClip
{
    public var CurrentLevel = "levelOne";

    public function Levels() 
    {
        addEventListener(Event.ADDED_TO_STAGE, onStage);

        trace("working");
    }
    private function onStage(event:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, onStage);
        trace("OnStage");
    }
    public function gotoLevelOne():void
    {
        var levelOne:LevelOne = new LevelOne();

        levelOne.x = stage.stageWidth * .5;
        levelOne.y = stage.stageHeight * .5;
        addChild(levelOne);

        CurrentLevel = "levelOne";
    }

}

}

我已经实例化了Levels。我的踪迹有效(onStage& working)。然而它仍然给了我

  

1009:null对象引用。哪个对象为null?

     

at Levels / gotoLevelOne()[C:... Flash Files \ Levels.as:28]

     

at Screens / newGameButton()[C:... \ Flash Files \ Screens.as:38]

所以行:

levelOne.x = stage.stageWidth * .5;

&安培;

levels.gotoLevelOne();

为什么levelOne为null?它不在显示列表中吗?当我实例化它时,我认为它已被添加到显示列表中。

2 个答案:

答案 0 :(得分:0)

尝试调试电影(ctrl + shift + enter)而不是运行它(ctrl + enter),当发生错误时你会看到行和堆栈跟踪,你会看到var是null。

我认为这是舞台,而你的剪辑不在舞台上,舞台变量为空。

你必须等待添加到舞台活动:

public function Screens() 
{
    // add the listener to init the menu position
    addEventListener( Event.ADDED_TO_STAGE, onStage );
}

private function onStage( event:Event ):void
{
    // remove the event listener
    removeEventListener( Event.ADDED_TO_STAGE, gotoMainMenu );

    // if there is currentScreen place it at the good place
    if( CurrentScreen )
    {
        CurrentScreen .x = stage.stageWidth * .5;
        CurrentScreen .y = stage.stageHeight * .5;
    }
}

public function gotoMainMenu():void
{
    // verify that stage exists so you will never got errors
    if( stage )
    {
        mainmenu.x = stage.stageWidth * .5;
        mainmenu.y = stage.stageHeight * .5;
    }

    addChild(mainmenu);

    mainmenu.newgame_btn.addEventListener(MouseEvent.MOUSE_DOWN, newGameButton);
    mainmenu.credits_btn.addEventListener(MouseEvent.MOUSE_DOWN, creditsButton);

    CurrentScreen = mainmenu;
}

修改

这样你就可以在需要时调用gotoMainMenu,这会更新CurrentScreen变量,当它被添加到舞台时,CurrentScreen将被置于好处地方

您可以在实例化后访问实例的每个公共方法和属性。

package  
{

    import flash.display.MovieClip;

    public class MainClass extends MovieClip 
    {
        // declare your property ( don't forget public, private, protected or internal )
        private var menus:Screens;

        public function MainClass() 
        {
            // create your menus
            menus = new Screens();

            // as the Screens object is not on the stage, gotoMainMenu not called so this will trace null here
            trace(menus.CurrentScreen);

            // add it to the display list to be sure to have it initialised
            addChild( menus );

            // now you can get access to preperties
            trace(menus.CurrentScreen);
        }
    }
}

答案 1 :(得分:0)

肯定是你在变量声明中实例化MainMenu。该阶段尚未定义,因此它会在gotoMainMenu中出现错误。阶段也未在类构造函数中定义,因为对象需要首先添加到显示列表中。因此,最好,如所述,使用ADDED_TO_STAGE并在那里实例化。

这也有其他好处,因为您可以实例化对象,并且在将对象添加到显示列表之前不要让它们执行任何操作。此外,在变量声明中实例化对象可能会降低代码速度。

要清楚,需要将一个对象添加到舞台或显示列表上的另一个对象,以使舞台不为空。