HTML and CSS Reference
In-Depth Information
called frombothfunctions.Thismightbealittle redundantinthisexample, butitcouldprove
useful in a larger game as a reusable function.
The onTouchMove function looks a little strange. This is because not all browsers give off the
same touch events. Some give a touch event off as an array, and some give it off as the actual
event. To make sure we cover as many devices as possible, we first look to see whether the
first element in the e.touches array exists. If it does, we use its attributes in the algorithm to
find the current touch location. If not, we use the attributes of the event passed in directly ( e ).
Beyond that, the touchX and touchY values are calculated in the same manner as the mouseX
and mouseY values for onMouseMove . We also need to make sure that the finger move event is
not passed to the mobile Safari web browser. This would result in the browser window mov-
ing rather than the ship moving. We do this with the e.preventDefault() function call.
NOTE
If you encounter mouseX and mouseY position problems when using the event.clientX and
event.clientY values on a scrolling browser window, you can substitute them with event.pageX
and event.page . In most browsers, these will help if the screen is scrolled.
Auto-fire
Oneotherchangewe'vemadeistoremovetheneedfortheplayertopressanykeysortapthe
screen to fire bullets. We have added this code to the updatePlayer() function.
player . missileFrameCount ++ ;
iif ( player . missileFrameCount > player . missileFrameDelay ){
playSound ( SOUND_SHOOT ,. 5 );
firePlayerMissile ();
player . missileFrameCount = 0 ;
}
The player.missileFrameCount and player.missileFrameDelay attributeswereaddedto
the player object in the gameStateNewgame() function from GeoBlaster Extended:
player . missileFrameDelay = 5 ;
player , missileFrameCount = 0 ;
Player movement
The player ship must now follow the mouse rather than respond to the arrow keys pressed by
theuser.Wehaveremoved checkKeys() functionfromtheGeoBlasterExtendedcodebaseas
Search WWH ::




Custom Search