④
expected to achieve the desired effect: http://www.html5china.com/html5games/mogu/index3.html
one to write a collision handler
JavaScript Code Copy to clipboard
function HasAnimalHitEdge () {/ / Bear hit the right border if (animal.x> screenWidth - animal.image.width) {if (horizontalSpeed> 0) / / if the right move horizontalSpeed =- horizontalSpeed ; / / change the direction of the horizontal velocity} / / Bear left across the border if (animal.x <-10) {if (horizontalSpeed <0) / / if left horizontalSpeed =-horizontalSpeed; / / change the direction of the horizontal velocity} / / Bear get the following boundary if (animal.y> screenHeight - animal.image.height) {/ / 2 seconds and start again setTimeout (function () {horizontalSpeed = speed; verticalSpeed =-speed; animal.x = parseInt (screenWidth / 2); animal.y = parseInt (screenHeight / 2); gameLoop ();}, 2000);} / / Bear hit above the border if (animal.y <0) {verticalSpeed =-verticalSpeed;}} Second, the game loop GameLoop () to add the rear boundary function testing, as
JavaScript Code Copy to clipboard
function GameLoop () {/ / clear the screen ctx.clearRect (0,Moncler Jackets UK, 0, screenWidth, screenHeight); ctx.save (); / / draw the background ctx.drawImage (backgroundForestImg,Moncler Jackets UK, 0,Moncler Jackets UK, 0); / / draw mushrooms ctx.drawImage (mushroom.image, mushroom.x, mushroom.y); / / draw the Bear / / change the X and Y position of the mobile animals animal.x + = horizontalSpeed; animal.y + = verticalSpeed; / / change roll angle animal.angle + = bearAngle; / / center of the current bear as the base ctx.translate (animal.x + (animal.image.width / 2), animal.y + (animal.image.height / 2) ); / / the angle of rotation according to the current bear ctx.rotate (animal.angle * Math.PI/180); / / description Bear ctx.drawImage (animal.image, - (animal.image.width / 2), - ( animal.image.height / 2)); ctx.restore (); / / test whether the encounter boundary HasAnimalHitEdge ();} The complete code for this fourth round is as follows:
folding XML / HTML Code Copy to clipboard
Chinese mushrooms moving-html5 Network < ; body>
support html5 browser to watch