Actionscript 3 某些视图中未显示全局按钮-MVC AS3

Actionscript 3 某些视图中未显示全局按钮-MVC AS3,actionscript-3,model-view-controller,view,Actionscript 3,Model View Controller,View,我正在编写一个程序,模拟一种旅行体验,用户可以在视图中键入城市名称,以MVC风格编写程序,然后用户被传输到一个视图,该视图用图片、信息等表示城市 如前所述,程序是用MVC编写的,我的主要问题如下: 我制作了一个全局按钮,不确定这是否是正确的定义,但从技术上讲,该按钮应该出现在我想要的每个视图中,如果单击该按钮,用户将返回到开始视图,在该视图中,用户可以键入新的城市名称等 我的问题是,按钮只出现在其中一个视图中,它们在代码上完全相似,只是名称不同,而在其他视图中则没有 这是创建按钮的代码,该按钮依

我正在编写一个程序,模拟一种旅行体验,用户可以在视图中键入城市名称,以MVC风格编写程序,然后用户被传输到一个视图,该视图用图片、信息等表示城市

如前所述,程序是用MVC编写的,我的主要问题如下:

我制作了一个全局按钮,不确定这是否是正确的定义,但从技术上讲,该按钮应该出现在我想要的每个视图中,如果单击该按钮,用户将返回到开始视图,在该视图中,用户可以键入新的城市名称等

我的问题是,按钮只出现在其中一个视图中,它们在代码上完全相似,只是名称不同,而在其他视图中则没有

这是创建按钮的代码,该按钮依赖于一个单独的类,该类在我的Main.as中给出了它的大小等:

    private function initMVC():void{

        goBackButton = new Button("Back");
        goBackButton.addEventListener(MouseEvent.CLICK, goBackButtonHandler);

        goToDestinationButton = new Button("");
        goToDestinationButton.addEventListener(MouseEvent.CLICK, goToDestionationButtonHandler);

        // Create the MODEL
        _model = new Model();

        // Create the CONTROLLERs which holds a reference of the MODEL
        _controller1 = new Controller1(_model);
        _controller2 = new Controller2(_model);

        // Create the VIEWs which holds references of both MODEL and CONTROLLER
        _view1 = new startView(_model, _controller1, goToDestinationButton);
        _view2 = new parisView(_model, _controller2, goBackButton);
        _view3 = new berlinView(_model, _controller2, goBackButton);
        _view4 = new copenhagenView(_model, _controller2, goBackButton);

        //add starting VIEW to Stage
        this.addChild(_view1);
    }

    private function goBackButtonHandler(event:MouseEvent):void
    {               
        this.removeChildAt(0);
        this.addChild(_view1);
    }
    private function goToDestionationButtonHandler(event:MouseEvent):void
    {               
        if (_model.name == "PARIS"){
        trace("Switching to View: Paris");
        this.removeChildAt(0);
        this.addChild(_view2);
        }
        else if (_model.name == "BERLIN"){
        trace("Switching to View: Berlin");
        this.removeChildAt(0);
        this.addChild(_view3);
        }
        else if (_model.name == "COPENHAGEN"){
        trace("Switching to View: Copenhagen");
        this.removeChildAt(0);
        this.addChild(_view4);
        }
        else{
        trace("We do not have that destination in our system yet, come back later.");
        }
以下是为表示城市而创建的其中一个视图的示例:

public class berlinView extends Sprite
{
    private var _navButton:Button;
    private var _model:Model;
    private var _controller:Controller2
    private var nametextField:TextField;

    public function berlinView(model:Model, controller:Controller2, navigationButton:Button)
    {
        _model= model;
        _controller = controller

        var atextField:TextField = new TextField();
        atextField.text = "This is Berlin";
        this.addChild(atextField);

        _navButton = navigationButton;
        _navButton.x = 100;
        _navButton.y = 100;
        this.addChild(_navButton);
    }
}
}

我可能忽略了一些非常简单或基本的东西,因为这是我第一个真正的AS3程序。希望有人能帮忙


提前谢谢

问题是按钮仅实例化一次,但在每个视图上都调用addChild,因此它将只添加到上次创建的视图中:\u view4

您应该这样更改视图构造函数:

public function berlinView(model:Model, controller:Controller2, navigationButton:Button)
{
    _model= model;
    _controller = controller

    var atextField:TextField = new TextField();
    atextField.text = "This is Berlin";
    this.addChild(atextField);

    _navButton = navigationButton;
    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
然后,仅在显示视图时添加按钮,即当视图添加到后台时:

private function onAddedToStage(event:Event):void
{
   _navButton.x = 100;
   _navButton.y = 100;
   this.addChild(_navButton);
}