AS3 - 如何在课堂上获取当前场景名称

时间:2014-03-03 07:51:30

标签: actionscript-3 flash

我正在玩flash,我已经为菜单,按钮等创建了多个场景。当尝试为一个场景中的按钮添加事件处理程序,而不是其他场景时,编译器抱怨说它不能引用不存在的对象。

我认为解决方案很简单...获取场景名称,将其与if语句匹配,并通过if语句加载事件处理程序......

然而,在网上挖了太久之后,我似乎无法找到一种方法来正确地做到这一点。有谁知道吗?

我尝试过使用以下内容:

var scene:Scene = myflvandclassname.currentScene;
var sName:String = MovieClip.currentScene.name;

Both lead to an error "Access of possibly undefined property Scene through a reference with static type Class".

2 个答案:

答案 0 :(得分:0)

忽略MovieClip和场景作为组织项目的源,以及时间轴上的代码。使用Document class作为切入点。这个tutorial应该帮助你掌握主要概念。

package {

    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;

    public class StackOverflow extends Sprite {
        public function StackOverflow() {
            addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }

        private function onAdded(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, onAdded);

            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            setup();
        }

        private function setup():void {
            const padding:int = 20;

            //Initiate your UI components and place them in the display list
            var menu:MyMenu = new MyMenu();
            var buttons:MyFooterButtons = new MyFooterButtons();
            var etc:AnotherComponent = new AnotherComponent();

            addChild(menu);
            addChild(buttons);
            addChild(etc);

            menu.x = menu.y = padding;
            //Place them and initialise with concrete information
        }
    }
}

例如, MyMenu,MyFooterButtons,AnotherComponent 可以是带有export settings的库中的MovieClip / Sprite,您可以使用展示位置,样式等完成所有工作

答案 1 :(得分:0)

我遇到了同样的问题。我想从外部类中检查当前场景名称并根据名称(游戏级别的名称)来传递一些属性中的某些值...所以我做了什么,它的工作原理是。

//main in 1st frame of the fla

stop();

var myCheckSceneClass: CheckSceneClass = new CheckSceneClass();
myCheckSceneClass.myCurrentScene = currentScene;
myCheckSceneClass.checkScene();

//CheckSceneClass


package {
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import flash.display.Scene;


    public class CheckSceneClass extends flash.display.MovieClip {

        public var myCurrentScene : Scene;



        public function CheckSceneClass () {


        }


        public function checkScene() {
            switch (myCurrentScene.name) {
                case "Scene 1":
trace("It is the 1st Scene");                   
break;
                default:
                    trace("Error with Scene Name");
                    break;

            }

        }








    }

}