RPGツクールMVでWindow_HorzCommandの使い方のまとめ

ツクールMVにはWindow_HorzCommandというクラスが用意されている。

Window_Commandと非常によく似た作りのクラスであり、使い方はほぼ同じ。

Window_HorzCommand の使い方

Window_Comamndとほぼ同じ。

違いは、numVisibleRows, maxCols といったメソッドが指定していることか。

class Window_SampleHorz extends Window_HorzCommand {

    // 選択肢を設定する。
    makeCommandList() {
        this.addCommand('はい', 'yes');
        this.addCommand('いいえ', 'no');
    }

    /**
     * Windowのwidthを指定できる。(option)
     */
    windowWidth() {
        return 240;
    };

    // 行の数。default: 1
    numVisibleRows() {
        return 1;
    };

    // 1行ごとの選択肢の数 default: 4
    maxCols() {
        return 4;
    };

    // 各選択肢の幅寄せ default: center。left とか right とか指定できる。
    itemTextAlign() {
        return 'center';
    };

}

使い方は、Window_Commandとほぼ同じ。

Scene_MenuBaseを継承したSceneクラスを用意して、そこに定義していく。

class Scene_Sample extends Scene_MenuBase {

    create(){
        super.create();

        // x: 10 y: 50 の位置にwindowを作成する。
        this._cancelConfirmWindow = new Window_SampleHorz(10, 50);

        // setHandlerでメニューが選択された時の挙動を指定する。
        this._cancelConfirmWindow.setHandler('yes', this.onCancelConfirmYes.bind(this));
        this._cancelConfirmWindow.setHandler('no', this.onCancelConfirmNo.bind(this));
        this._cancelConfirmWindow.setHandler('cancel', this.onCancelConfirmNo.bind(this));

        this.addWindow(this._cancelConfirmWindow);
    }
}

また、cancelというシンボルの設定もしているが、これは右クリックをした時などの挙動を指定できるもの。

およそ、handlerで呼びだす関数でやることは何かしらの処理をした後に、あるwindowは閉じてあるwindowは開くといった 行為になる。

onCancelConfirmNo() {
    this._cancelConfirmWindow.close();
    this._questListWindow.open();
    this._questListWindow.activate();

    // メニューから抜け出す時など
    SceneManager.pop();
}

他のWindowクラスとの比較

基本的に選択肢を作成する場合はWindow_Commandクラスを使い、 横長の選択肢を作りたい場合は、Window_HorzCommandとなるだろう。

使いみちとしては、メニュー画面で使うことになる。