我创建了一个chack
键盘点击函数,而不是一直声明事件监听器。
当你调用它一次它工作但当我有两个或更多实例时,只有最后一个工作正常。
var left = y_input_s.chack (y_key.left);
var right = y_input_s.chack (y_key.right);
只有“正确”有效。
这是功能:
y_input.prototype.chack = function (key)
{
//38 up |40 down| 39 left | right 37 | space 32 |
//insert key code to array dinamicly
//if its unudetfid so the key wasent pressed yet so its false
if(y_input.prototype.key_down_cack[key] == undefined)
{
y_input.prototype.key_down_cack[key] = false;
}
//if the key is down change its place in array to true
document.onkeydown = function(event)
{
if(event.keyCode == key){y_input.prototype.key_down_cack[key] = true;}
}
//if the key is up change its place in array to false
document.onkeyup = function(event)
{
if(event.keyCode == key){y_input.prototype.key_down_cack[key] = false;}
}
//return the key state from array
return y_input.prototype.key_down_cack[key] ;
}//end chack
我测试了函数内部的东西,“key”的值很好,但是当我在里面破解“key”时。
document.onkeydown = function(event)
{
它返回chack
参数中传递的最后一个函数键值。