gpt4 book ai didi

javascript - 模拟按键事件按下,强制按住向下箭头?

转载 作者:行者123 更新时间:2023-12-03 00:55:50 25 4
gpt4 key购买 nike

我正在做一个非常基本的挑战,用一个简单的 JS 脚本击败 Chrome 恐龙游戏。您可以在 chrome://dino/或 https://chromedino.com/ 找到它。我的跳跃/仙人掌事件调度工作正常,但我的“鸭子”按住而不只是按下,我不知道如何调试/找出为什么它按住而不只是按下。

任何帮助将不胜感激,这只是一个简单的小项目,没什么严重的

// A JS script made for the offline Chrome dino game. Accessible from either chrome://dino/ or https://chromedino.com/ 
var canvas = document.getElementsByTagName("canvas")[0],
ctx = canvas.getContext("2d"); // Return the drawing context of the canvas

(function recurse(){
// Reduce the Uint8ClampedArray into a number. If it isn't zero, there's an obstacle
// This is the cactus jump
let cactus = (ctx.getImageData(85,118,60,1).data).reduce(reducer);
// This is the duck, when the Pterodactyl comes
let pterodactyl = (ctx.getImageData(90,85,1,1 ).data).reduce(reducer);
if(cactus){
var e = new KeyboardEvent('keydown',{'keyCode':38,'which':38});
document.dispatchEvent(e);
console.log(cactus)
} else if(pterodactyl){
var e = new KeyboardEvent('keydown',{'keyCode':40,'which':40});
document.dispatchEvent(e);
}
setTimeout(recurse, 50);
})();

function reducer(a,b){
return a + b
}

我已经检查过键盘事件没有从 https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent 触发 KeyboardEvent.repeat但事实并非如此!

最佳答案

那么也许可以尝试使用 keypress 而不是 keydown

那为什么跳跃没问题呢?我不知道你的jump是什么,但我敢打赌,jump Action 不能被按住,不像duck,只要按下键就可以躲避

希望对你有帮助

关于javascript - 模拟按键事件按下,强制按住向下箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com