1

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

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

我制作了一个全局按钮(不确定这是否是正确的定义,但从技术上讲,该按钮也应该出现在我想要的每个视图中),如果单击它会将用户带回开始视图(用户可以在其中输入新的城市名称ETC)。

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

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

提前致谢!

4

1 回答 1

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);
}
于 2013-05-23T13:20:27.370 回答