jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of

02.06.2013 Views

[ 299 ] Chapter 10 We also declare some more local variables here including bulletPos which is the current position of the bullet plus half of the width of the ship, and an array to hold the x and y coordinates of the bullet. The values for this array are set to the bulletPos variable for the x position, and directly above the nose of the ship for the y position. We also store the length of the aliens array as a local variable for use in a for loop once again. We define an inline function along with our variables called fire(). This function is used in conjunction with an interval to create the motion of the bullet. Within this function, we check that the bullet hasn't reached the top of the canvas, and provided it hasn't, that is if its y position is greater than 0, we remove the bullet with the clearRect() function, then update the values in the bulletPos array and draw the bullet in its new location using the updated values from the array. Once the position of the bullet has been updated, we then need to check whether the bullet, in its new position, has collided with an alien or not, so we use a for loop to iterate over each alien in the aliens array. On each iteration we first check whether the bullet falls within the y axis of an alien, that is whether its position is less than the bottom edge of an alien, but more than its top edge. The aliens are positioned according to their top-left corner, so to work out whether the bullet has passed its bottom edge we just add the height of an alien to its y position. If the bullet does fall within the alien on the y axis, we then check whether it falls within the space an alien is taking up along the x axis. If it does, we remove the alien from the canvas with the clearRect() function and splice the alien out of the array so that it stays removed. We then remove the bullet from the canvas using the clearRect() function again, and clear the bulletInt interval. If there are no more aliens left, we clear the interval producing the motion of the aliens, reset the dirCounter variable, reduce the alienSpeed variable by 100, and then call the addAliens() function to redraw the aliens at the top of the canvas. This is effectively how the player moves up to the next level, and each time the aliens are redrawn they move faster, creating basic progression of the game. This now brings us to the end of the code. If we run the game now in a standard-compliant browser such as Firefox or Chrome, we should find that we have a perfectly playable game, implemented entirely using JavaScript and the element.

Canvas Animations Pop quiz – creating canvas-based games 1. In this example a lot of functionality that related to the player's space ship was put into an onload event handler. Why? a. Because we cannot interact with an image until it has loaded completely b. To make the code work correctly in Internet Explorer c. Because the code runs faster once the image has finished loading d. To help make our code more modular 2. Why did we set the textAlign property of the canvas to center when writing the GAME OVER message? a. Setting the alignment is a prerequisite for writing text to the canvas b. Because it is easier than working out the width of the text and then setting its position on the x axis in order to position the text in the center of the canvas c. To anti-alias the text d. Because it is more efficient than using padding Have a go hero – extending the space invaders clone Our game is a much simpler version of the original space invaders. The original arcade game had many other features including aliens that fired back at the player's ship, bases to hide behind, and one off special aliens that appeared randomly throughout the game and dropped bonuses when hit. Certainly one thing that the game needs is a scoring mechanism, otherwise there is simply no incentive to play. Implement a scoring system that tracks a player's score throughout the game and saves the highest score to the player's machine. This could be done easily with jQuery and the cookie plugin, or using LocalStorage. I'd also urge you, as this is the last example of the book, to implement some of the other missing features, such as giving the aliens the ability to fire back, and adding bases or shields that the player can hide beneath when the going gets tough. [ 300 ]

Canvas <strong>Animation</strong>s<br />

Pop quiz – creating canvas-based games<br />

1. In this example a lot <strong>of</strong> functionality that related to the player's space ship was put<br />

into an onload event handler. Why?<br />

a. Because we cannot interact with an image until it has loaded completely<br />

b. To make the code work correctly in Internet Explorer<br />

c. Because the code runs faster once the image has finished loading<br />

d. To help make our code more modular<br />

2. Why did we set the textAlign property <strong>of</strong> the canvas to center when writing the<br />

GAME OVER message?<br />

a. Setting the alignment is a prerequisite for writing text to the canvas<br />

b. Because it is easier than working out the width <strong>of</strong> the text and then setting its<br />

position on the x axis in order to position the text in the center <strong>of</strong> the canvas<br />

c. To anti-alias the text<br />

d. Because it is more efficient than using padding<br />

Have a go hero – extending the space invaders clone<br />

Our game is a much simpler version <strong>of</strong> the original space invaders. The original arcade<br />

game had many other features including aliens that fired back at the player's ship, bases to<br />

hide behind, and one <strong>of</strong>f special aliens that appeared randomly throughout the game and<br />

dropped bonuses when hit.<br />

Certainly one thing that the game needs is a scoring mechanism, otherwise there is simply<br />

no incentive to play. Implement a scoring system that tracks a player's score throughout the<br />

game and saves the highest score to the player's machine. This could be done easily with<br />

<strong>jQuery</strong> and the cookie plugin, or using LocalStorage.<br />

I'd also urge you, as this is the last example <strong>of</strong> the book, to implement some <strong>of</strong> the other<br />

missing features, such as giving the aliens the ability to fire back, and adding bases or shields<br />

that the player can hide beneath when the going gets tough.<br />

[ 300 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!