Game Development Reference
In-Depth Information
Figure 3-4. Color Drop win message
This was your first real game-like example so far, and although extremely basic, you're starting use your newly
learned skills to create interactive content. You have a few more things to learn to improve the interactive experience
in games, one of which is the keyboard and how you can use it to control game objects.
Keyboard Events
The keyboard is a crucial component to computer game development. It's the next best, tactical piece of hardware
after the video game controller. Many would argue it's even better.
Many of the games in this topic will be mobile friendly so mouse events, which double as tap events, will often
times be your only choice in creating an interactive experience. Desktop games, however, don't have that limitation
and plenty of game developers have no desire to even reach the mobile space at all. That developer might be you,
so let's dive into the keyboard controls that you'll be using in the game project in the next chapter.
Keyboard events are similar to mouse events and you set them up pretty much identically. The difference is that
you typically add the keyboard event listeners to the HTML document or window object, as opposed to display objects
directly. The following are two approaches to handling keyboard events:
//approach 1
window.addEventListener('keydown',function(e){
//move hero
});
//approach 2
window.onkeydown = moveHero;
function moveHero(e){
//move hero
};
 
Search WWH ::




Custom Search