<aside> 💡 本系列教學主要帶你一步步了解RMMZ腳本框架,建議有基本JS基礎再進行閱讀

</aside>

上一篇我們我們說明了SceneManager的運作內容,這樣接下來的閱讀就會更容易了解,「為什麼可以這樣寫?」

承接上上篇,再次讓我們回到SceneManager.run(Scene_Boot) 這邊吧!

SceneManager.run(Scene_Boot)

透過這個function 我們要進入第一個遊戲畫面 可以看到這邊使用了 this.goto(sceneClass)

SceneManager.run = function(sceneClass) {
    try {
        this.initialize();
        this.goto(sceneClass);
        Graphics.startGameLoop();
    } catch (e) {
        this.catchException(e);
    }
};

跳到goto 這個function,將 this._nextScene 賦值成 new sceneClass() ,也就是上面提到的new Scene_Boot()

並將this._scene(當前的scene) 進行stop

SceneManager.goto = function(sceneClass) {
    if (sceneClass) {
        this._nextScene = new sceneClass();
    }
    if (this._scene) {
        this._scene.stop();
    }
};

接著來看到上一篇提到的 SceneManager.update中的 changeScene 這邊是每一幀都會進行update的function,可以看到在 this._nextScene 有值的狀況下會執行create 進行切換 scene。

// isSceneChanging 將會return this._exiting || !!this._nextScene;
SceneManager.changeScene = function() {
    if (this.isSceneChanging() && !this.isCurrentSceneBusy()) {
        if (this._scene) {
            this._scene.terminate();
            this.onSceneTerminate();
        }
        this._scene = this._nextScene;
        this._nextScene = null;
        if (this._scene) {
            this._scene.create();
            this.onSceneCreate();
        }
        if (this._exiting) {
            this.terminate();
        }
    }
};

直到下個frame的時候,就會對新的scene開始 進行update 也就是說我們成功切換了Scene

SceneManager.updateScene = function() {
    if (this._scene) {
        if (this._scene.isStarted()) {
            if (this.isGameActive()) {
                this._scene.update();
            }
        } else if (this._scene.isReady()) {
            this.onBeforeSceneStart();
            this._scene.start();
            this.onSceneStart();
        }
    }
};