02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[ 299 ]<br />

Chapter 10<br />

We also declare some more local variables here including bulletPos which is the current<br />

position <strong>of</strong> the bullet plus half <strong>of</strong> the width <strong>of</strong> the ship, and an array to hold the x and y<br />

coordinates <strong>of</strong> the bullet. The values for this array are set to the bulletPos variable for the<br />

x position, and directly above the nose <strong>of</strong> the ship for the y position. We also store the length<br />

<strong>of</strong> the aliens array as a local variable for use in a for loop once again.<br />

We define an inline function along with our variables called fire(). This function is used<br />

in conjunction with an interval to create the motion <strong>of</strong> the bullet. Within this function, we<br />

check that the bullet hasn't reached the top <strong>of</strong> the canvas, and provided it hasn't, that is if<br />

its y position is greater than 0, we remove the bullet with the clearRect() function, then<br />

update the values in the bulletPos array and draw the bullet in its new location using the<br />

updated values from the array.<br />

Once the position <strong>of</strong> the bullet has been updated, we then need to check whether the bullet,<br />

in its new position, has collided with an alien or not, so we use a for loop to iterate over<br />

each alien in the aliens array.<br />

On each iteration we first check whether the bullet falls within the y axis <strong>of</strong> an alien, that<br />

is whether its position is less than the bottom edge <strong>of</strong> an alien, but more than its top edge.<br />

The aliens are positioned according to their top-left corner, so to work out whether the<br />

bullet has passed its bottom edge we just add the height <strong>of</strong> an alien to its y position.<br />

If the bullet does fall within the alien on the y axis, we then check whether it falls within<br />

the space an alien is taking up along the x axis. If it does, we remove the alien from the<br />

canvas with the clearRect() function and splice the alien out <strong>of</strong> the array so that it<br />

stays removed.<br />

We then remove the bullet from the canvas using the clearRect() function again,<br />

and clear the bulletInt interval. If there are no more aliens left, we clear the interval<br />

producing the motion <strong>of</strong> the aliens, reset the dirCounter variable, reduce the<br />

alienSpeed variable by 100, and then call the addAliens() function to redraw<br />

the aliens at the top <strong>of</strong> the canvas.<br />

This is effectively how the player moves up to the next level, and each time the aliens are<br />

redrawn they move faster, creating basic progression <strong>of</strong> the game. This now brings us to the<br />

end <strong>of</strong> the code. If we run the game now in a standard-compliant browser such as Firefox or<br />

Chrome, we should find that we have a perfectly playable game, implemented entirely using<br />

JavaScript and the element.

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

Saved successfully!

Ooh no, something went wrong!