未捕获的类型错误:不能设置未定义的属性。

时间:2022-08-24 13:54:57

Google Chrome is throwing "Uncaught TypeError: Cannot set property 'isDown' of undefined" but it doesn't look like anything is wrong with my code!

谷歌Chrome正在抛出“Uncaught TypeError: can 'isDown' of undefined”,但是看起来我的代码没有任何问题!

Important part of my variable array:

我的变量数组的重要部分:

KEY = {
    UP: 38,
    DOWN: 40,
    W: 87,
    S: 83,
    D: 68
}
pingpong = {
    fps: 60,
    pressedKeys: [],
    complete: false,
}

Key listener initialization (this is where the error is thrown):

关键监听器初始化(这是抛出错误的地方):

    for (var keyCode in KEY) {
        if (KEY.hasOwnProperty(keyCode)) {
            pingpong.pressedKeys[KEY[keyCode]] = {
                isDown: false,
                wasDown: false
            };
        }
    }
    $(document).keydown(function(e) {
        pingpong.pressedKeys[e.which].isDown = true;
    });
    $(document).keyup(function(e) {
/* This line is the issue */    pingpong.pressedKeys[e.which].isDown = false;
    });

Any Ideas?

什么好主意吗?

2 个解决方案

#1


2  

The problem is that you're trying to access an element of the pressedKeys array which does not exist. For example, if we pressed the "a" key:

问题是,您正在尝试访问一个不存在的pressedKeys数组的元素。例如,如果我们按下“a”键:

$(document).keyup(function(e) {
    //Pressed "a" so e.which == 65
    pingpong.pressedKeys[e.which].isDown = false;
});

When you initialize your array you only create elements for the properties of the KEY object:

初始化数组时,只为键对象的属性创建元素:

for (var keyCode in KEY) {
    //Iterating over KEY, which contains 5 properties...
    if (KEY.hasOwnProperty(keyCode)) {
        //Add value to pressedKeys (this happens for each of the 5 properties)
        pingpong.pressedKeys[KEY[keyCode]] = {
            isDown: false,
            wasDown: false
        };
    }
}

So pressedKeys only contains 5 elements, corresponding to the properties of KEY. Note that a TypeError is also thrown in the keydown event handler, as well as keyup.

所以按下键只包含5个元素,对应于键的属性。注意,在keydown事件处理程序中也会抛出一个TypeError,以及keyup。

To fix it, you could check that e.which is in the KEYS object in the event handler functions. If it's not, just ignore it. Something like this perhaps (there may be a better way, this is just what came to mind first):

为了修正它,你可以检查e。在事件处理程序函数的键对象中。如果不是,就忽略它。也许有这样的事情(也许有更好的办法,这就是我首先想到的):

$(document).keydown(function(e) {
    for(var k in KEY) {
        if(KEY[k] == e.which) {
            break; //Break out of loop and execute last line
        }
        return false; //Key not recognized, last line is not executed
    }
    pingpong.pressedKeys[e.which].isDown = true;
});

#2


1  

e.which is IE only. Real browsers use e.keyCode

e。这是只支持IE。真正的浏览器使用e.keyCode

jQuery Event Keypress: Which key was pressed?

jQuery事件按键:按下哪个键?

#1


2  

The problem is that you're trying to access an element of the pressedKeys array which does not exist. For example, if we pressed the "a" key:

问题是,您正在尝试访问一个不存在的pressedKeys数组的元素。例如,如果我们按下“a”键:

$(document).keyup(function(e) {
    //Pressed "a" so e.which == 65
    pingpong.pressedKeys[e.which].isDown = false;
});

When you initialize your array you only create elements for the properties of the KEY object:

初始化数组时,只为键对象的属性创建元素:

for (var keyCode in KEY) {
    //Iterating over KEY, which contains 5 properties...
    if (KEY.hasOwnProperty(keyCode)) {
        //Add value to pressedKeys (this happens for each of the 5 properties)
        pingpong.pressedKeys[KEY[keyCode]] = {
            isDown: false,
            wasDown: false
        };
    }
}

So pressedKeys only contains 5 elements, corresponding to the properties of KEY. Note that a TypeError is also thrown in the keydown event handler, as well as keyup.

所以按下键只包含5个元素,对应于键的属性。注意,在keydown事件处理程序中也会抛出一个TypeError,以及keyup。

To fix it, you could check that e.which is in the KEYS object in the event handler functions. If it's not, just ignore it. Something like this perhaps (there may be a better way, this is just what came to mind first):

为了修正它,你可以检查e。在事件处理程序函数的键对象中。如果不是,就忽略它。也许有这样的事情(也许有更好的办法,这就是我首先想到的):

$(document).keydown(function(e) {
    for(var k in KEY) {
        if(KEY[k] == e.which) {
            break; //Break out of loop and execute last line
        }
        return false; //Key not recognized, last line is not executed
    }
    pingpong.pressedKeys[e.which].isDown = true;
});

#2


1  

e.which is IE only. Real browsers use e.keyCode

e。这是只支持IE。真正的浏览器使用e.keyCode

jQuery Event Keypress: Which key was pressed?

jQuery事件按键:按下哪个键?