From f74458cc5c1d9664306c55fbc10b8f687a055114 Mon Sep 17 00:00:00 2001 From: Double-X Date: Thu, 18 Jun 2020 17:12:00 +0800 Subject: [PATCH] Adds a function to check if a key's just released Added this._isReleased = {}; in Input.clear Added the following in Input.update: else if (this._previousState[name] && !this._currentState[name]) { this._isReleased[name] = true; } else { this._isReleased[name] = false; } Right after: if (this._currentState[name] && !this._previousState[name]) { this._latestButton = name; this._pressedTime = 0; this._date = Date.now(); } Added Input.isReleased --- js/rpg_core/Input.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/js/rpg_core/Input.js b/js/rpg_core/Input.js index 9c305c59..a4acf903 100644 --- a/js/rpg_core/Input.js +++ b/js/rpg_core/Input.js @@ -108,6 +108,7 @@ Input.clear = function() { this._dir8 = 0; this._preferredAxis = ''; this._date = 0; + this._isReleased = {}; }; /** @@ -128,12 +129,32 @@ Input.update = function() { this._latestButton = name; this._pressedTime = 0; this._date = Date.now(); + } else if (this._previousState[name] && !this._currentState[name]) { + this._isReleased[name] = true; + } else { + this._isReleased[name] = false; } this._previousState[name] = this._currentState[name]; } this._updateDirection(); }; +/** + * Checks whether a key is just released. + * + * @static + * @method isTriggered + * @param {String} keyName The mapped name of the key + * @return {Boolean} True if the key is released + */ +Input.isReleased = function(keyName) { + if (this._isEscapeCompatible(keyName) && this.isTriggered('escape')) { + return true; + } else { + return !!this._isReleased[keyName]; + } +}; + /** * Checks whether a key is currently pressed down. *