Jump to content

KeyCode error?


Dr.Linux
 Share

Recommended Posts

 

 

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>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...