{"id":442,"date":"2013-06-24T17:06:57","date_gmt":"2013-06-24T17:06:57","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=442"},"modified":"2013-06-24T17:07:43","modified_gmt":"2013-06-24T17:07:43","slug":"create-html5-canvas-shooter-part4-keyboard-interaction","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/create-html5-canvas-shooter-part4-keyboard-interaction\/","title":{"rendered":"How to create a HTML5 Canvas shooter \u2013 Part4: Keyboard Interaction"},"content":{"rendered":"<h2>Adding the new functions<\/h2>\n<p>To make our HTML5 game more interactive we need to add three new functions to allow some movement with the w, s, a, d keys. They will be used to handle when a key is pressed down, when a key is released and one small function to update these values in the game loop. So add the following above the <em>mouseMove()<\/em> function<\/p>\n<div>\n<div id=\"highlighter_872577\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<\/td>\n<td>\n<div>\n<div><code>function<\/code> <code>keyPress(e){<\/code><\/div>\n<div><\/div>\n<div><code>}<\/code><\/div>\n<div><\/div>\n<div><code>function<\/code> <code>keyRelease(e){<\/code><\/div>\n<div><\/div>\n<div><code>}<\/code><\/div>\n<div><\/div>\n<div><code>function<\/code> <code>move(){<\/code><\/div>\n<div><\/div>\n<div><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>We also need to add 5 new variables that these functions will need to use.<\/p>\n<div>\n<div id=\"highlighter_327018\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<\/td>\n<td>\n<div>\n<div><code>var<\/code> <code>main_x = 0, main_y = 0, move_x = 0, move_y = 0;<\/code><\/div>\n<div><code>var<\/code> <code>speed = 3;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>When a key is pressed down<\/h2>\n<p>JavaScript needs the key code of each key to know what has been pressed. We will use D:68, A:65, S:83 and W:87. You can find all of the codes right here <a href=\"http:\/\/www.cambiaresearch.com\/articles\/15\/javascript-char-codes-key-codes\">http:\/\/www.cambiaresearch.com\/articles\/15\/javascript-char-codes-key-codes<\/a><\/p>\n<p>So in the keyPress() function add the following code to handle each key press<\/p>\n<div>\n<div id=\"highlighter_624361\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<\/td>\n<td>\n<div>\n<div><code>function<\/code> <code>keyPress(e){<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 68){ <\/code><code>\/\/d<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 65){ <\/code><code>\/\/a<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 83){ <\/code><code>\/\/s<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}\u00a0\u00a0\u00a0 <\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 87){ <\/code><code>\/\/w<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The reason we are keeping each if statement separate and not using if else is because we want our functions to handle multiple key presses at the same time. This will allow our shape to move up and left or down and right.<\/p>\n<p>We need to fill these if statements with some code, we will update our new variables which will later be passed to our shape. Update the function to look like this.<\/p>\n<div>\n<div id=\"highlighter_886615\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<\/td>\n<td>\n<div>\n<div><code>function<\/code> <code>keyPress(e){<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 68){ <\/code><code>\/\/d<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>move_x = speed;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 65){ <\/code><code>\/\/a<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>move_x = -speed;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 83){ <\/code><code>\/\/s<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>move_y = speed;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}\u00a0\u00a0\u00a0 <\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 87){ <\/code><code>\/\/w<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>move_y = -speed;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The reason for the speed variable is to make it easy to change the speed if we wish to. As you can see we are updating move_x to move horizontaly and move_y to move vertically.<\/p>\n<h2>When a key is released<\/h2>\n<p>Next we will make the key release function, now we have made the key press function this will be really simple to do. All we need is to reset the value of move_x and move_y to 0 so the shape is no longer updated with the value passed by the speed variable, and will in turn stop moving.<\/p>\n<div>\n<div id=\"highlighter_789682\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<\/td>\n<td>\n<div>\n<div><code>function<\/code> <code>keyRelease(e){<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 68){ <\/code><code>\/\/d<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>move_x = 0;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 65){ <\/code><code>\/\/a<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>move_x = 0;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 83){ <\/code><code>\/\/s<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>move_y = 0;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}\u00a0\u00a0\u00a0 <\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>if<\/code><code>(e.keyCode == 87){ <\/code><code>\/\/w<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>move_y = 0;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>The Move() function<\/h2>\n<p>We have two other variables we have not used yet, main_x and main_y. In the move() function we will update this value with move_x and move_y.<\/p>\n<div>\n<div id=\"highlighter_272428\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<\/td>\n<td>\n<div>\n<div><code>function<\/code> <code>move(){<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>main_x += move_x;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>main_y += move_y;<\/code><\/div>\n<div><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>As you can see we are using += instead of just plus this will add the value of the move variables to the current value of the main variables. So the shape will not sporadically jump about the canvas.<\/p>\n<h2>Finishing up<\/h2>\n<p>The shape will still not move because we have not done anything with the values or added event listeners to fire our functions. In the mouseMove() function update the following lines of code.<\/p>\n<div>\n<div id=\"highlighter_785536\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<\/td>\n<td>\n<div>\n<div><code>mouseX = mouseX - (top_canvas.width\/2); <\/code><\/div>\n<div><code>mouseY = mouseY - (top_canvas.height\/2);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Just minus the value of main_x and main_y so you are left with.<\/p>\n<div>\n<div id=\"highlighter_276236\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<\/td>\n<td>\n<div>\n<div><code>mouseX = mouseX - (top_canvas.width\/2) - main_x; <\/code><\/div>\n<div><code>mouseY = mouseY - (top_canvas.height\/2) - main_y;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>At the bottom of the init() function add the following event listeners<\/p>\n<div>\n<div id=\"highlighter_229533\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<\/td>\n<td>\n<div>\n<div><code>window.addEventListener(<\/code><code>\"keydown\"<\/code><code>, keyPress, <\/code><code>false<\/code><code>);<\/code><\/div>\n<div><code>window.addEventListener(<\/code><code>\"keyup\"<\/code><code>, keyRelease, <\/code><code>false<\/code><code>);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Finally add the move() function to the gameLoop() this will make those values update in time with the rest of the game.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding the new functions To make our HTML5 game more interactive we need to add three new functions to allow some movement with the w, s, a, d keys. They will be used to handle when a key is pressed down, when a key is released and one small function to update these values in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[22,7],"tags":[],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/442"}],"collection":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}