AS3:从另一个类调用switch case不起作用

时间:2012-04-28 04:20:55

标签: actionscript-3 class function flash-cs5 switch-statement

我在flash中制作一个游戏,而我遇到的问题是两个类,一个名为Menu的类,用于欢迎屏幕,它有一个startgame按钮和引擎类,当我第一次运行游戏时它会运行一切我将菜单设置为由此代码自动显示:

    public var state:int;
    public const MENU:int = 0;
    public const GAME:int = 1;
    // create the variable for the menu
    private var _menu:Menu;

    public var sBg1:ScrollBg;
    public var sBg2:ScrollBg;
    public function Engine(menu:Menu) 
    {

        _menu = menu;
        //we add event listener when the engine is added to the stage
        addEventListener(Event.ADDED_TO_STAGE, onAdded);
    }
    private function onAdded(e:Event):void {
        //then we remove the event listener and initiate the init function
        removeEventListener(Event.ADDED_TO_STAGE, onAdded);

        init();
    }
    private function init():void {
        //the init function set the game state at first to menu and run the drawUI function
        state = MENU;
        drawUI();

    }

    public function drawUI():void 
    {
        //the drawUI function draw the needed elements on stage according to the state of the game
        switch(state){
            case MENU:
                _menu = new Menu();
                addChild(_menu);
                break;
            case GAME:
                sBg1= new ScrollBg();   
                addChild(sBg1);
                sBg1.x = 0;
                sBg1.y = 0;
                //if(contains(_menu)){
                //removeChild(_menu);}

                trace('this the game');
                break;
                    }
    }

我使用菜单类中的代码将状态从菜单更改为实际游戏:

    public var start_btn:MovieClip;
    private var _engine:Engine;
    public function Menu() 
    {
        addEventListener(Event.ADDED_TO_STAGE, displayMenu);
    }

    private function displayMenu(e:Event):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, displayMenu);
        start_btn = new startBtn();
        start_btn.x = 100;
        start_btn.y = 200;
        addChild(start_btn);
        start_btn.addEventListener(MouseEvent.CLICK, startGame);
    }

    private function startGame(e:MouseEvent):void 
    {
        start_btn.removeEventListener(MouseEvent.CLICK, startGame);
        _engine = new Engine(this);
        _engine.state = 1;
        _engine.drawUI();
    }

我每次都使用drawUI函数进行菜单,例如它放置开始按钮,然后为游戏放置背景但由于某种原因我只得到跟踪语句说(这是游戏)但是背景没有显示任何线索。

我花了4个多小时试图找出问题所在,如果有人能帮助我,我仍然无法做到这一点。

由于

1 个答案:

答案 0 :(得分:0)

如果您在屏幕上看到跟踪但没有任何内容,因为ScrollBg类没有任何要显示的内容。确保该类中包含一些精灵或图像。