Game Development Reference
In-Depth Information
Chapter 3
Capturing User Input
Now that you have a solid understanding of how graphics are created and how you can animate them, some player
interaction is in order to turn your visuals into playable, interactive content.
I'll discuss using both mouse and keyboard controls to capture user input, and you'll view a few practical
examples that show how to accomplish some common gaming features. Text will also be introduced to display the
necessary messaging to provide feedback to the player.
Mouse Events
EaselJS not only handles your canvas drawing, but also the management of mouse positions and actions, and whether
they intersect with any of your display objects. This allows you to mimic mouse interactions. Starting with the utmost
basics of all interactivity, we'll start by handling some clicks on some simple shape objects.
Mouse Clicks
A mouse event is set up and handled like many other typical interactive environments you might already be used to,
such as Flash or HMTL DOM. An event listener is applied to the display object for a specific type of mouse interaction,
and an event handler is written to accept and handle it.
troll.addEventListener('click',trollAttacked);
function trollAttacked(e){
alert('ouch');
}
In this troll attacking example, you add the event listener directly on your display object, and you write your
function to handle it.
Another way to achieve this would be to use an anonymous function as your event handler.
troll.addEventListener('click', function (e) {
alert('ouch');
});
 
Search WWH ::




Custom Search