Dr.Linux Posted August 16, 2014 Share Posted August 16, 2014 I got a code to work, but in the console it's giving me a Uncaught Error: Cannot read property 'Keycode' of undefined. Here's the code.<!DOCTYPE html><!-- saved from url=(0055)http://www.homeandlearn.co.uk/JS/move_box_keyboard.html --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>The Canvas Tag</title> </head> <body> <header> <p>Press TAB to begin, W, A, S, D keys to move</p> </header> <canvas width="150" height="400" id="canvas_1" tabindex="0"> Canvas tag not supported </canvas> </section> <script> var canvas = document.getElementById("canvas_1"); canvas.addEventListener('keyup', Update, true); canvas_context = canvas.getContext("2d"); var x = 0; var y = 0; function Game(){ Draw(); Update(); } function Draw(){ clearCanvas(); canvas_context.fillRect(x, y, 50, 50); } function Update(e){ //==================== // THE W KEY //==================== if (e.keyCode == 87) { y = y - 50; } //==================== // THE S KEY //==================== if (e.keyCode == 83) { y = y + 50; } //==================== // THE A KEY //==================== if (e.keyCode == 65) { x = x - 50; } //==================== // THE D KEY //==================== if (e.keyCode == 68) { x = x + 50; } } function clearCanvas() { canvas.width = canvas.width; } setInterval(Game, 1); </script> </body></html> Quote Link to comment Share on other sites More sharing options...
OadT Posted August 16, 2014 Share Posted August 16, 2014 Simply remove the "Update"-Call from the "Game"-function. if you call the update-function inside the game-function the parameter e is undefined inside the update-function. You already added an event-listener to call the update-function when the event keyup is fired Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.