1

ユーザーがビューに都市名を入力できる旅行体験をシミュレートするプログラムを作成しています (MVC スタイルでプログラムを作成する) と、ユーザーは写真、情報で都市を表すビューに「移動」します。等

前に述べたように、プログラムは MVC で書かれており、私の主な問題は次のとおりです。

グローバル ボタンを作成しました (これが正しい定義かどうかはわかりませんが、技術的には、必要なすべてのビューにボタンを表示する必要があります)。クリックすると、ユーザーが開始ビューに戻ります (ユーザーが新しい都市名を入力できる場所)。等)。

私の問題は、ボタンがビューの 1 つにのみ表示され (名前の違いを除けば、コードは完全に似ています)、他のビューには表示されないことです。

これは、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.");
        }

以下は、都市を表すために作成されたビューの 1 つの例です。

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 に答える