本帖最后由 zths 于 2020-4-10 21:37 编辑
/** * Checks whether a key is just pressed. * * @static * @method isTriggered * @param {String} keyName The mapped name of the key * @return {Boolean} True if the key is triggered */ Input.isTriggered = function(keyName) { if (this._isEscapeCompatible(keyName) && this.isTriggered('escape')) { return true; } else { return this._latestButton === keyName && this._pressedTime === 0; } };
/**
* Checks whether a key is just pressed.
*
* @static
* @method isTriggered
* @param {String} keyName The mapped name of the key
* @return {Boolean} True if the key is triggered
*/
Input.isTriggered = function(keyName) {
if (this._isEscapeCompatible(keyName) && this.isTriggered('escape')) {
return true;
} else {
return this._latestButton === keyName && this._pressedTime === 0;
}
};
上面是游戏的 isTriggered 逻辑
可以看到 他是 取得最后一个按下的键,并且是该帧按下的。才会返回 true...
所以他叫 isTriggered , 表达的意思是 在该帧被触发了.
我写的是浏览器原生方法,不需要 mv 核心的参与..
实现是判断被下的时间大于(所需值)....
如果需要修改为和他一样的实现的的话. 需要把 setInterval 的 func hook 至 图形更新方法下面。。
然后判断的时候把 大于 改为 等于 就可以了。。。(时间处理部分也需要修改成帧计数)
/** * Checks whether a key is currently pressed down. * * @static * @method isPressed * @param {String} keyName The mapped name of the key * @return {Boolean} True if the key is pressed */ Input.isPressed = function(keyName) { if (this._isEscapeCompatible(keyName) && this.isPressed('escape')) { return true; } else { return !!this._currentState[keyName]; } };
/**
* Checks whether a key is currently pressed down.
*
* @static
* @method isPressed
* @param {String} keyName The mapped name of the key
* @return {Boolean} True if the key is pressed
*/
Input.isPressed = function(keyName) {
if (this._isEscapeCompatible(keyName) && this.isPressed('escape')) {
return true;
} else {
return !!this._currentState[keyName];
}
};
我审题有点问题。因为我实现的是 isPressed...
顺便
/** * Checks whether a key is kept depressed. * * @static * @method isLongPressed * @param {String} keyName The mapped name of the key * @return {Boolean} True if the key is long-pressed */ Input.isLongPressed = function(keyName) { if (this._isEscapeCompatible(keyName) && this.isLongPressed('escape')) { return true; } else { return (this._latestButton === keyName && this._pressedTime >= this.keyRepeatWait); } };
/**
* Checks whether a key is kept depressed.
*
* @static
* @method isLongPressed
* @param {String} keyName The mapped name of the key
* @return {Boolean} True if the key is long-pressed
*/
Input.isLongPressed = function(keyName) {
if (this._isEscapeCompatible(keyName) && this.isLongPressed('escape')) {
return true;
} else {
return (this._latestButton === keyName &&
this._pressedTime >= this.keyRepeatWait);
}
};
对着这个逻辑改改你就能得到你所有想要的了。。。 |