Error in pong script.

Started by
11 comments, last by luveti 10 years, 10 months ago

As i think i fixed the error being canvas missed <canvas id="pongGame"

dry.png


And then

Uncaught ReferenceError: onKeyDown is not defined

Advertisement

Javascript functions don't work like that. Eigther store the onKeyXXX-functions in an object (then you can reference them by the objects name) or register them directely in those lines as anonymous functions.

I took a look at your code and after fiddling with it for a while it seems to run fine in chrome. You should store key presses in a buffer though, here's an example:


//a buffer of boolean values, to tell us whether or not the key is down
var keys = [];

//attach your event listeners
document.onkeydown = function(e){
    keys[e.keyCode] = true;
};
document.onkeyup = function(e){
    keys[e.keyCode] = false;
};

//now when you want to check if a key is down just do this
if(keys[40] == true){
    //do something
}
This method of getting input allows you to store keypresses a lot easier, for a list of all the javascript keycodes I recommend going here -> http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

This topic is closed to new replies.

Advertisement