Game Development Reference
In-Depth Information
Now, every time the mouse is moved, the handleMouseMove function is called. You can put
instructions in this function to extract the mouse position from the evt object. For example, this
event-handler function retrieves the mouse's x-position and y-position and stores them in the
variable balloonPosition :
function handleMouseMove(evt) {
Game.balloonPosition = { x : evt.pageX, y : evt.pageY };
}
The pageX and pageY member variables of the evt object contain the position of the mouse relative
to the page, meaning the top-left corner of the page has coordinates (0, 0). You can see a few
examples of mouse positions in Figure 5-1 : three of the corners are labeled with their respective
positions as reported when running the program in a browser.
Figure 5-1. Mouse positions for the upper-left, upper-right, and lower-right corners
Because the Draw method simply draws the balloon at the mouse position, the balloon now follows
the mouse. Figure 5-2 shows what that looks like. You can see the balloon drawn underneath the
mouse pointer; it will track the pointer as you move it.
 
Search WWH ::




Custom Search