查看: 4147|回复: 5

[有事请教] 判断某文件是否存在,用条件分歧的脚本怎么写?

[复制链接]
梦石
8
星屑
3963
在线时间
397 小时
注册时间
2017-10-11
回帖
114
发表于 2020-6-28 03:15:31 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
例如,判断某个文件名的图片是否存在,存在则显示,不存在则跳过。
梦石
0
星屑
7802
在线时间
683 小时
注册时间
2012-7-29
回帖
172
发表于 2020-6-28 05:23:21 | 显示全部楼层
我可能会用这段代码来实现判断文件是否存在:
  1. require('fs').existsSync('img/animations/Absorb.png');
复制代码

但是对于1楼所描述,遇到不存在的图片就跳过的效果,我认为另一个方法实现得更加简便。方法是让显示图片命令遇到不存在的图片时不报错。

要实现图片缺失却不报错,可以试试下面这个插件:
来源:https://raw.githubusercontent.com/triacontane/RPGMakerMV/master/ThroughFailedToLoad.js
插件本体
  1. //=============================================================================
  2. // ThroughFailedToLoad.js
  3. // ----------------------------------------------------------------------------
  4. // Copyright (c) 2015-2017 Triacontane
  5. // This software is released under the MIT License.
  6. // [url]http://opensource.org/licenses/mit-license.php[/url]
  7. // ----------------------------------------------------------------------------
  8. // Version
  9. // 2.3.1 2017/10/30 アニメーション画像に対するエラーが無視が無効になっていた問題を修正
  10. // 2.3.0 2017/10/29 音声ファイルと画像ファイルのいずれかのみ無視する機能を追加
  11. // 2.2.0 2017/06/18 本体v1.5.0で機能しなくなる問題を修正
  12. // 2.1.1 2017/03/11 通常版1.3.5でエラーになる問題を修正
  13. // 2.1.0 2017/03/11 本体v1.3.5(コミュニティ版)で機能しなくなる問題を修正
  14. // 2.0.0 2016/08/05 本体v1.3.0対応(1.2.0では使えなくなります)
  15. // 1.0.0 2016/06/25 初版
  16. // ----------------------------------------------------------------------------
  17. // [Blog]   : [url]https://triacontane.blogspot.jp/[/url]
  18. // [Twitter]: [url]https://twitter.com/triacontane/[/url]
  19. // [GitHub] : [url]https://github.com/triacontane/[/url]
  20. //=============================================================================

  21. /*:
  22. * @plugindesc Through Failed to load
  23. * @author triacontane
  24. *
  25. * @param InvalidIfTest
  26. * @desc Not through if test play.
  27. * @default true
  28. * @type boolean
  29. *
  30. * @param InvalidIfWeb
  31. * @desc Not through if Web mode.
  32. * @default false
  33. * @type boolean
  34. *
  35. * @param ThroughType
  36. * @desc 無視する素材の種別です。
  37. * @default 3
  38. * @type select
  39. * @option Audio Only
  40. * @value 1
  41. * @option Image Only
  42. * @value 2
  43. * @option All
  44. * @value 3
  45. *
  46. * @help Through error of Failed to load.
  47. * Image not found, Audio not found.
  48. *
  49. * This plugin is released under the MIT License.
  50. */
  51. /*:ja
  52. * @plugindesc ロード失敗エラーのすり抜けプラグイン
  53. * @author トリアコンタン
  54. *
  55. * @param テストプレー時無効
  56. * @desc テストプレー時は本プラグインの機能が無効になります。
  57. * @default true
  58. * @type boolean
  59. *
  60. * @param Web版で無効
  61. * @desc Web版実行時は本プラグインの機能が無効になります。
  62. * @default false
  63. * @type boolean
  64. *
  65. * @param 無視種別
  66. * @desc 無視する素材の種別です。
  67. * @default 3
  68. * @type select
  69. * @option 音声のみ
  70. * @value 1
  71. * @option 画像のみ
  72. * @value 2
  73. * @option 全て
  74. * @value 3
  75. *
  76. * @help 存在しない画像、音声素材が指定された場合に発生するエラーを無視します。
  77. * 音声の場合は何も再生されず、画像の場合は空の透明画像がセットされます。
  78. *
  79. * エラーログは通常通り出力されます。
  80. *
  81. * 本体v1.5.0より正式に実装されたロード失敗時に3回までリロードする機能については
  82. * 当プラグインの機能と競合するため、無効化されます。
  83. *
  84. * フォント、データベースまたはプラグインで追加されたファイルの読み込みに
  85. * 失敗した場合は、通常通りエラーが発生します。
  86. *
  87. * このプラグインにはプラグインコマンドはありません。
  88. *
  89. * 利用規約:
  90. *  作者に無断で改変、再配布が可能で、利用形態(商用、18禁利用等)
  91. *  についても制限はありません。
  92. *  このプラグインはもうあなたのものです。
  93. */

  94. (function() {
  95.     'use strict';
  96.     var pluginName = 'ThroughFailedToLoad';

  97.     var getParamOther = function(paramNames) {
  98.         if (!Array.isArray(paramNames)) paramNames = [paramNames];
  99.         for (var i = 0; i < paramNames.length; i++) {
  100.             var name = PluginManager.parameters(pluginName)[paramNames[i]];
  101.             if (name) return name;
  102.         }
  103.         return null;
  104.     };

  105.     var getParamBoolean = function(paramNames) {
  106.         var value = (getParamOther(paramNames) || '').toUpperCase();
  107.         return value === 'ON' || value === 'TRUE';
  108.     };

  109.     var getParamNumber = function(paramNames, min, max) {
  110.         var value = getParamOther(paramNames);
  111.         if (arguments.length < 2) min = -Infinity;
  112.         if (arguments.length < 3) max = Infinity;
  113.         return (parseInt(value) || 0).clamp(min, max);
  114.     };

  115.     //=============================================================================
  116.     // パラメータの取得と整形
  117.     //=============================================================================
  118.     var paramInvalidIfTest = getParamBoolean(['InvalidIfTest', 'テストプレー時無効']);
  119.     var paramInvalidIfWeb  = getParamBoolean(['InvalidIfWeb', 'Web版で無効']);
  120.     var paramThroughType   = getParamNumber(['ThroughType', '無視種別'], 1, 3);

  121.     //=============================================================================
  122.     // プラグイン無効条件の判定
  123.     //=============================================================================
  124.     if (paramInvalidIfTest && Utils.isOptionValid('test')) {
  125.         return;
  126.     } else if (paramInvalidIfWeb && !Utils.isNwjs()) {
  127.         return;
  128.     }

  129.     if (paramThroughType !== 1) {
  130.         //=============================================================================
  131.         // Bitmap
  132.         //  エラー発生用のフラグをキャンセルします。
  133.         //=============================================================================
  134.         var _Bitmap_isReady = Bitmap.prototype.isReady;
  135.         Bitmap.prototype.isReady = function() {
  136.             if (this.isError()) {
  137.                 this.eraseError();
  138.             }
  139.             return _Bitmap_isReady.apply(this, arguments);
  140.         };

  141.         var _Bitmap_decode = Bitmap.prototype.decode;
  142.         Bitmap.prototype.decode = function(){
  143.             _Bitmap_decode.apply(this, arguments);
  144.             if (this._loadingState === 'requesting') {
  145.                 this._image.addEventListener('error', this._onError.bind(this));
  146.             }
  147.         };

  148.         Bitmap.prototype.eraseError = function() {
  149.             this._hasError     = false;
  150.             this._isLoading    = false;
  151.             this._loadingState = 'loaded';
  152.         };

  153.         //=============================================================================
  154.         // Graphics
  155.         //  エラーイベントを登録します。
  156.         //=============================================================================
  157.         var _Graphics__playVideo = Graphics._playVideo;
  158.         Graphics._playVideo      = function(src) {
  159.             _Graphics__playVideo.apply(this, arguments);
  160.             this._video.onerror = this._videoLoader || this._onVideoError.bind(this);
  161.         };
  162.     }

  163.     if (paramThroughType !== 2) {
  164.         //=============================================================================
  165.         // AudioManager
  166.         //  エラーチェック処理を無視します。
  167.         //=============================================================================
  168.         AudioManager.checkErrors = function() {};
  169.     }

  170.     if (typeof ResourceHandler !== 'undefined') {
  171.         //=============================================================================
  172.         // ResourceHandler
  173.         //  リトライ機能の仕様を変更します。
  174.         //=============================================================================
  175.         var _ResourceHandler_createLoader = ResourceHandler.createLoader;
  176.         ResourceHandler.createLoader = function(url, retryMethod, resignMethod, retryInterval) {
  177.             return this.isNeedLoader(url) ? _ResourceHandler_createLoader.apply(this, arguments) : null;
  178.         };

  179.         ResourceHandler.isNeedLoader = function(url) {
  180.             if (paramThroughType === 1 && !url.match(/^audio\//)) {
  181.                 return true;
  182.             } else if (paramThroughType === 2 && (!url.match(/^img\//) && !url.match(/^movie\//))) {
  183.                 return true;
  184.             } else {
  185.                 return false;
  186.             }
  187.         };
  188.     }
  189. })();

复制代码

这样一来,就可以在事件执行内容中随心所欲地显示图片,遇到不存在的图片也不会报错了。这个方法省去了写条件分歧的工作量。

评分

参与人数 1赞 +1 收起 理由
CCD0645 + 1 认可答案

查看全部评分

回复

使用道具 举报

老黄鸡

梦石
4
星屑
46140
在线时间
7978 小时
注册时间
2009-7-6
回帖
13343

MZ评测员RM创作大赛01组委会开拓者贵宾

发表于 2020-6-28 08:40:42 | 显示全部楼层
给bitmap加一坨函数

  1. Bitmap.prototype.setErrorListener = function(listner) {
  2.         if (!this.isError() && this._image) {
  3.                 this._image.removeEventListener('error', this._errorListener);
  4.                 this._image.addEventListener('error', this._errorListener = listner);
  5.         } else {
  6.                 listner(this);
  7.         }
  8. };
复制代码


测试
  1. var callback = function() {
  2.   console.log('8存在');
  3. };
  4. var bitmap = ImageManager.loadPicture("test");
  5. bitmap.setErrorListener(callback.bind(this));
复制代码


当然,这个方法是异步的,不一定能立刻获得结果
所以一般加一个addLoadListener来确认是否成功
RGDirect - DirectX驱动的RGSS,点我了解.
(排满,暂停)RM全系列成套系统定制请联系QQ1213237796
不接受对其他插件维护的委托
回复

使用道具 举报

梦石
0
星屑
1343
在线时间
153 小时
注册时间
2020-4-26
回帖
146
发表于 2020-6-28 08:46:10 | 显示全部楼层
但以遊戲開發的角度,什麼情況會出現,圖片資源存在的不肯定呢?會用到,你就會放,他就該在
因為fs是node.js的模組,打包的方式不支援就不能用

点评

同意,不如研究检查文件完整性的方案来得实在。不过也许楼主是想做ARG游戏,让玩家去改文件名,改对了才显示。这就需要判断文件存在与否了。  发表于 2020-6-28 12:23
回复

使用道具 举报

梦石
2
星屑
2701
在线时间
204 小时
注册时间
2020-5-21
回帖
116

RM创作大赛01参赛者

发表于 2020-6-28 09:44:44 | 显示全部楼层
耶,收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部