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

时间:2013-05-23 08:56:03

标签: actionscript-3 model-view-controller view

我正在编写一个模拟旅行体验的程序,用户可以在视图中键入城市名称(以MVC风格编写程序),然后用户“运送”到代表城市的视图图片,信息等。

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

我创建了一个全局按钮(不确定这是否是rigth定义,但从技术上讲,按钮应出现在我想要的每个视图中),如果点击,则会将用户带回到开始视图(用户可以输入新城市名称等)。

我的问题是该按钮只出现在其中一个视图中(除了名称的不同之外,它们在代码中的atm完全相似)而在其他视图中没有。

这是在我的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程序。希望有人可以帮忙!

提前致谢!

1 个答案:

答案 0 :(得分:0)

问题是该按钮只被实例化一次,但您在每个视图上调用addChild,因此它只会被添加到上一个创建的视图中:_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);
}