Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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 AS3不允许我从其他.as文件访问类_Actionscript 3_Flash_Class_Packages_Flash Cs6 - Fatal编程技术网

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

Actionscript 3 AS3不允许我从其他.as文件访问类,actionscript-3,flash,class,packages,flash-cs6,Actionscript 3,Flash,Class,Packages,Flash Cs6,我有MainClass.as作为我的文档类。然后我创建了菜单。现在,我希望这些菜单位于不同的文件(包…idk)中 这是我在屏幕上的新游戏按钮。如下所示: private function newGameButton(Event:MouseEvent):void { var levels:Levels = new Levels(); stage.focus = stage; removeChild(CurrentScreen);

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

这是我在屏幕上的新游戏按钮。如下所示:

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

        stage.focus = stage;
        removeChild(CurrentScreen);
        levels.gotoLevelOne();
    }
以下是级别。例如:

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

}
}

我已经实例化了级别。我的跟踪工作(舞台和工作)。但它仍然给了我机会

1009:空对象引用。哪个对象是空的

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

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

因此,这些行:

levelOne.x = stage.stageWidth * .5;
&

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

尝试调试电影(ctrl+shift+enter)而不是运行它(ctrl+enter),当出现错误时,您将获得行和堆栈跟踪,并且您将看到其中的var为null

我认为这是stage,当您的剪辑不在stage上时,stage变量为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
变量,当它添加到stage时,
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);
        }
    }
}

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

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


为了清楚起见,需要将一个对象添加到stage或显示列表上的另一个对象,以便stage不为null。

\Screens.as,第51 1061行:通过引用静态类型flash调用可能未定义的方法removeEventListener。事件:事件。如果我删除事件。它停止拍摄错误,跟踪“工作”,不显示我的屏幕。如果我添加gotoMainMenu();在ADDED_TO_屏幕侦听器下,它需要一个参数。啊,抱歉,只需按如下方式编辑代码:
removeEventListener(Event.ADDED_TO_STAGE,gotoMainMenu)谢谢谢谢谢谢谢谢!!!!!我不知道你必须添加child()。现在有道理了。但是如果我需要调用函数“gotoMainMenu()”,我应该把什么作为参数呢?(我的信用卡菜单中有一个“返回主菜单”按钮。)我是否需要调整一个新函数并调用该事件函数(“gotoMainMenu”)“FirstScreenLoad”或其他什么?对参数有点困惑。好吧,也许你不想为添加的_to_satge处理程序调用这个函数gotoMainMenu(),但可能是init(),让你的gotoMainMenu函数保持公共状态,在没有参数调用之后,我将编辑前面的答案,告诉你如何在我实例化“Screens”之后如何使用“MainClass”中的那些函数?每次我尝试时,它都会说未定义-新手
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);
        }
    }
}