gpt4 book ai didi

Javascript 事件处理崩溃

转载 作者:行者123 更新时间:2023-11-28 08:40:21 25 4
gpt4 key购买 nike

我目前正在学习 JS,因此我认为与其使用键盘库,不如自己制作一个键盘库,这样我可以更好地了解它是如何工作的。我正在创建一个游戏,它实际上并不是一个仅在按下键盘时更新游戏的选项,因此我创建了一个循环来运行库,我将使用它来更新玩家位置。问题是它不断崩溃,我认为这与创建和关闭事件太多有关。

运行库的相关代码将返回一个 key :

function Keyboard(){
ArcKeyboard.ActiveKeys();
};
setInterval(function(){Keyboard()},200);

图书馆:

var ArcKeyboard

(function(ArcKeyboard){
var Key = {
1: ["No Key Assigned",0],
2: ["No Key Assigned",0],
3: ["No Key Assigned",0],
4: ["No Key Assigned",0],
5: ["No Key Assigned",0],
6: ["No Key Assigned",0],
7: ["No Key Assigned",0],
8: ["backspace",0],
9: ["tab",0],
10: ["No Key Assigned",0],
11: ["No Key Assigned",0],
12: ["No Key Assigned",0],
13: ["enter",0],
14: ["No Key Assigned",0],
15: ["No Key Assigned",0],
16: ["shift",0],
17: ["ctrl",0],
18: ["alt",0],
19: ["pause",0],
20: ["capslock",0],
21: ["No Key Assigned",0],
22: ["No Key Assigned",0],
23: ["No Key Assigned",0],
24: ["No Key Assigned",0],
25: ["No Key Assigned",0],
26: ["No Key Assigned",0],
27: ["esc",0],
28: ["No Key Assigned",0],
29: ["No Key Assigned",0],
30: ["No Key Assigned",0],
31: ["No Key Assigned",0],
32: ["space",0],
33: ["pageup",0],
34: ["pagedown",0],
35: ["end",0],
36: ["home",0],
37: ["left",0],
38: ["up",0],
39: ["right",0],
40: ["down",0],
41: ["No Key Assigned",0],
42: ["No Key Assigned",0],
43: ["No Key Assigned",0],
44: ["No Key Assigned",0],
45: ["insert",0],
46: ["delete",0],
47: ["No Key Assigned",0],
48: ["0",0],
49: ["1",0],
50: ["2",0],
51: ["3",0],
52: ["4",0],
53: ["5",0],
54: ["6",0],
55: ["7",0],
56: ["8",0],
57: ["9",0],
58: ["No Key Assigned",0],
59: ["No Key Assigned",0],
60: ["No Key Assigned",0],
61: ["No Key Assigned",0],
62: ["No Key Assigned",0],
63: ["No Key Assigned",0],
64: ["No Key Assigned",0],
65: ["a",0],
66: ["b",0],
67: ["c",0],
68: ["d",0],
69: ["e",0],
70: ["f",0],
71: ["g",0],
72: ["h",0],
73: ["i",0],
74: ["j",0],
75: ["k",0],
76: ["l",0],
77: ["m",0],
78: ["n",0],
79: ["o",0],
80: ["p",0],
81: ["q",0],
82: ["r",0],
83: ["s",0],
84: ["t",0],
85: ["u",0],
86: ["v",0],
87: ["w",0],
88: ["x",0],
89: ["y",0],
90: ["z",0],
91: ["windowskey",0],
92: ["No Key Assigned",0],
93: ["menukey",0],
94: ["No Key Assigned",0],
95: ["No Key Assigned",0],
96: ["keypad0",0],
97: ["keypad1",0],
98: ["keypad2",0],
99: ["keypad3",0],
100: ["keypad4",0],
101: ["keypad5",0],
102: ["keypad6",0],
103: ["keypad7",0],
104: ["keypad8",0],
105: ["keypad9",0],
106: ["keypad*",0],
107: ["keypad+",0],
108: ["No Key Assigned",0],
109: ["keypad-",0],
110: ["keypad.",0],
111: ["keypad/",0],
112: ["f1",0],
113: ["f2",0],
114: ["f3",0],
115: ["f4",0],
116: ["f5",0],
117: ["f6",0],
118: ["f7",0],
119: ["f8",0],
120: ["f9",0],
121: ["f10",0],
122: ["f11",0],
123: ["f12",0],
124: ["No Key Assigned",0],
125: ["No Key Assigned",0],
126: ["No Key Assigned",0],
127: ["No Key Assigned",0],
128: ["No Key Assigned",0],
129: ["No Key Assigned",0],
130: ["No Key Assigned",0],
131: ["No Key Assigned",0],
132: ["No Key Assigned",0],
133: ["No Key Assigned",0],
134: ["No Key Assigned",0],
135: ["No Key Assigned",0],
136: ["No Key Assigned",0],
137: ["No Key Assigned",0],
138: ["No Key Assigned",0],
139: ["No Key Assigned",0],
140: ["No Key Assigned",0],
141: ["No Key Assigned",0],
142: ["No Key Assigned",0],
143: ["No Key Assigned",0],
144: ["numlock",0],
145: ["scrolllock",0],
146: ["No Key Assigned",0],
147: ["No Key Assigned",0],
148: ["No Key Assigned",0],
149: ["No Key Assigned",0],
150: ["No Key Assigned",0],
151: ["No Key Assigned",0],
152: ["No Key Assigned",0],
153: ["No Key Assigned",0],
154: ["No Key Assigned",0],
155: ["No Key Assigned",0],
156: ["No Key Assigned",0],
157: ["No Key Assigned",0],
158: ["No Key Assigned",0],
159: ["No Key Assigned",0],
160: ["No Key Assigned",0],
161: ["No Key Assigned",0],
162: ["No Key Assigned",0],
163: ["No Key Assigned",0],
164: ["No Key Assigned",0],
165: ["No Key Assigned",0],
166: ["No Key Assigned",0],
167: ["No Key Assigned",0],
168: ["No Key Assigned",0],
169: ["No Key Assigned",0],
170: ["No Key Assigned",0],
171: ["No Key Assigned",0],
172: ["No Key Assigned",0],
173: ["No Key Assigned",0],
174: ["No Key Assigned",0],
175: ["No Key Assigned",0],
176: ["No Key Assigned",0],
177: ["No Key Assigned",0],
178: ["No Key Assigned",0],
179: ["No Key Assigned",0],
180: ["No Key Assigned",0],
181: ["No Key Assigned",0],
182: ["No Key Assigned",0],
183: ["No Key Assigned",0],
184: ["No Key Assigned",0],
185: ["No Key Assigned",0],
186: [";",0],
187: ["=",0],
188: [",",0],
189: ["-",0],
190: [".",0],
191: ["/",0],
192: ["'",0],
193: ["No Key Assigned",0],
194: ["No Key Assigned",0],
195: ["No Key Assigned",0],
196: ["No Key Assigned",0],
197: ["No Key Assigned",0],
198: ["No Key Assigned",0],
199: ["No Key Assigned",0],
200: ["No Key Assigned",0],
201: ["No Key Assigned",0],
202: ["No Key Assigned",0],
203: ["No Key Assigned",0],
204: ["No Key Assigned",0],
205: ["No Key Assigned",0],
206: ["No Key Assigned",0],
207: ["No Key Assigned",0],
208: ["No Key Assigned",0],
209: ["No Key Assigned",0],
210: ["No Key Assigned",0],
211: ["No Key Assigned",0],
212: ["No Key Assigned",0],
213: ["No Key Assigned",0],
214: ["No Key Assigned",0],
215: ["No Key Assigned",0],
216: ["No Key Assigned",0],
217: ["No Key Assigned",0],
218: ["No Key Assigned",0],
219: ["[",0],
220: ["\\",0],
221: ["]",0],
222: ["#",0],
223: ["No Key Assigned",0],
224: ["No Key Assigned",0],
225: ["No Key Assigned",0],
226: ["No Key Assigned",0],
227: ["No Key Assigned",0],
228: ["No Key Assigned",0],
229: ["No Key Assigned",0],
230: ["No Key Assigned",0],
231: ["No Key Assigned",0],
232: ["No Key Assigned",0],
233: ["`",0]
};

var KeysPressed ="";
var Counter = 0;
var ActiveKeys = function(){
document.addEventListener('keydown', function(event) {
Listener(event.keyCode,"keydown");
}, false);
document.addEventListener('keyup', function(event) {
Listener(event.keyCode,"keyup");
}, false);

document.removeEventListener('keydown',function(event){
Listener(event.keyCode,"keydown");
},false);
document.removeEventListener('keyup',function(event){
Listener(event.keyCode,"keyup");
},false);
}

var Listener = function(key,mode){
var i = 1;
var j = 1;
if(mode=="keydown"){
if(Key[key][1]==0){
console.log(Key[key][0]);
Key[key][1]=1;
}
}else if(mode=="keyup"){
if(Key[key][1]>0){
console.log(Key[key][0]);
Key[key][1]=0;
}
}
}

//Export Library
ArcKeyboard.ActiveKeys=ActiveKeys;
})(ArcKeyboard || (ArcKeyboard={}));

最佳答案

请更改以下内容:

在开始时将变量赋值给函数

var ArcKeyboard = (function(){
...

最后返回公共(public)函数

    //Export Library
return {
ActiveKeys:ActiveKeys
};
})();

这里有一个工作示例:http://jsfiddle.net/6rP37/

干杯!

关于Javascript 事件处理崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564186/

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