Converting Keyboard Events into Avatar Movement

Show your code again, then remove the alert(event.code) line inside the document.addEventListener. Replace it with the following:

​ document.addEventListener(​'keydown'​, sendKeyDown);
​ ​function​ sendKeyDown(event) {
» ​var​ code = event.code;
» ​if​ (code == ​'ArrowLeft'​) avatar.position.x = avatar.position.x - 5;
» ​if​ (code == ​'ArrowRight'​) avatar.position.x = avatar.position.x + 5;
» ​if​ (code == ​'ArrowUp'​) avatar.position.z = avatar.position.z - 5;
» ​if​ (code == ​'ArrowDown'​) avatar.position.z = avatar.position.z + 5;
​ }

We’ll talk about if, == (is it equal?) and = (make it equal) in Chapter 7, ​A Closer Look at JavaScript Fundamentals ...

Get 3D Game Programming for Kids, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.