查看: 1834|回复: 0

[交流讨论] 【JS应用题第二弹】怎么在标题画面添加退出游戏选项捏

[复制链接]

21

主题

12

金币

2597

贝壳

旅行者

Rank: 1

积分
361

明灯云会员

发表于 2023-9-21 16:19:18 | 显示全部楼层 |阅读模式
本帖最后由 康娜酱 于 2023-9-21 17:23 编辑

1. 新建一个MZ工程。下载一个JS编辑器,比如VS Code。用编辑器打开工程的js文件夹

2. 整理出目前已知的东西(记笔记):标题画面有一个窗口,內有「重新开始,继续游戏,选项」这三个选项,这些选项的文本可以在「数据库 - 用语」中修改

3.「数据库 - 用语」中的內容,均可以从一个叫TextManager的东西中读取记笔记

4. JS编辑器中全局搜索TextManager,会发现有211个搜索结果
接下来就到体现日常学习英文在阅读MV源码上的重要性了,选项的英文是option(这个字应该中小学有教吧?不然玩黄油时也有看过吧2333333
使用VS Code的正则模式搜索TextManager.*option或者option.*TextManager,前者搜出2个搜索结果,后者搜出0个搜索结果
(正则的应用在RM中十分常见,建议有时间&兴趣的萌新,百度学习一下相关知识

5. 在搜出的2个搜索结果中,一个在Window_MenuCommand,一个在Window_TitleCommand
那么如何判断出哪行才是使标题窗口带有「选项」选项的呢?可以在各行前用//来把这行变成绿色~
先//掉Menu的,进游戏看效果,标题没变化,//掉Title的,进游戏看效果,标题的「选项」选项的不见了!

因此咱们可以得出「Window_TitleCommand这行this.addCommand(TextManager.options, "options");是令标题画面窗口有「选项」选项的因素」这么的一个重点记笔记

6. 之后再搜索一下这行中的"options",搜到另外的4个搜索结果,分別位于Scene_Title,Scene_Menu,Window_MenuCommand
Menu的三个搜索结果从上面第5步得知理应是不用管了,所以Scene_Title这行this._commandWindow.setHandler("options", this.commandOptions.bind(this));是另一个重点记笔记

7. 之后再搜索一下这行中的commandOptions,搜到另外的3个搜索结果,分別位于Scene_Title,Scene_Menu
Menu那两个搜索结果又不用管,看看Scene_Title的
    this._commandWindow.close();
    SceneManager.push(Scene_Options);
第一行Window跟close,Window是窗口,close是关闭,那么这行的作用就是关闭某个窗口(标题画面默认只有一个窗口)
第二行SceneManager.push(Scene_xxxxx);是mvmz中比较常用的东西,比如SceneManager.push(Scene_Item)可以打开背包,SceneManager.push(Scene_Load)可以打开读档画面,SceneManager.exit()可以退出游戏记笔记

8. 到编写插件的时候了,先在Project1\js\plugins內新建一个js文件,随便命名成TitleExit.js,插件內文复制贴上以下的基本格式
基本格式
//=============================================================================
// aaaaaaaaaa
//=============================================================================

/*:
* @target MZ
* @plugindesc bbbbbbbbbb
* @author cccccccccc
* @url xxxxxxxxxx
*
* @help yyyyyyyyyy
*/

(() => {

})();
收起


9. 之后把上方VS Code搜到的內容先复制下来变成
// 省略......

(() => {

    Window_TitleCommand.prototype.makeCommandList = function () {
        const continueEnabled = this.isContinueEnabled();
        this.addCommand(TextManager.newGame, "newGame");
        this.addCommand(TextManager.continue_, "continue", continueEnabled);
        this.addCommand(TextManager.options, "options");
    };

    Scene_Title.prototype.createCommandWindow = function () {
        const background = $dataSystem.titleCommandWindow.background;
        const rect = this.commandWindowRect();
        this._commandWindow = new Window_TitleCommand(rect);
        this._commandWindow.setBackgroundType(background);
        this._commandWindow.setHandler("newGame", this.commandNewGame.bind(this));
        this._commandWindow.setHandler("continue", this.commandContinue.bind(this));
        this._commandWindow.setHandler("options", this.commandOptions.bind(this));
        this.addWindow(this._commandWindow);
    };

    Scene_Title.prototype.commandOptions = function () {
        this._commandWindow.close();
        SceneManager.push(Scene_Options);
    };

})();
收起


记笔记在每个需要保留原有效果的函数,
先在xxx.prototype.yyy = function () {的上一行写上const _xxx_yyy = xxx.prototype.yyy;
再清空內容,然后加上_xxx_yyy.apply(this, arguments);

记笔记不需要保留原有效果的函数,把函数重新命名,按需求变更函数內容

10. makeCommandList与createCommandWindow需要保留,commandOptions不需要保留
// 省略......

(() => {

    const _Window_TitleCommand_makeCommandList = Window_TitleCommand.prototype.makeCommandList;
    Window_TitleCommand.prototype.makeCommandList = function () {
        _Window_TitleCommand_makeCommandList.apply(this, arguments);
    };

    const _Scene_Title_createCommandWindow = Scene_Title.prototype.createCommandWindow;
    Scene_Title.prototype.createCommandWindow = function () {
        _Scene_Title_createCommandWindow.apply(this, arguments);
    };

    Scene_Title.prototype.commandExit = function () {
        SceneManager.exit();
    };

})();
收起


11. 最后加入上方提及的两个重点
// 省略......

(() => {

    const _Window_TitleCommand_makeCommandList = Window_TitleCommand.prototype.makeCommandList;
    Window_TitleCommand.prototype.makeCommandList = function () {
        _Window_TitleCommand_makeCommandList.apply(this, arguments);
        this.addCommand(TextManager.gameEnd, "exit");
    };

    const _Scene_Title_createCommandWindow = Scene_Title.prototype.createCommandWindow;
    Scene_Title.prototype.createCommandWindow = function () {
        _Scene_Title_createCommandWindow.apply(this, arguments);
        this._commandWindow.setHandler("exit", this.commandExit.bind(this));
    };

    Scene_Title.prototype.commandExit = function () {
        SceneManager.exit();
    };

})();
收起


自此得到了一个能在标题画面退出游戏的插件
TitleExit.rar (640 Bytes, 下载次数: 5)

总结一下本次教学所能学习到的RM知识:
1. 备有一个JS编辑器
2. 整理出目前已知的东西
3. TextManager存着「数据库 - 用语」的內容
4. 从已知的东西不停地搜索出未知的东西
5. //的妙用
6. SceneManager.push(Scene_xxxxx);的多种用法
7. SceneManager.exit()可以退出游戏




回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表