jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
[ 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 ]
- Page 269 and 270: CSS3 Animations The context of this
- Page 271 and 272: CSS3 Animations Time for action - i
- Page 273 and 274: CSS3 Animations The first variable
- Page 275 and 276: CSS3 Animations Time for action - a
- Page 277 and 278: Download from Wow! eBook CSS3 Anim
- Page 279 and 280: CSS3 Animations We also have to upd
- Page 281 and 282: CSS3 Animations flatMatrix[3] = fla
- Page 283 and 284: CSS3 Animations When updating the s
- Page 285 and 286: CSS3 Animations translate translate
- Page 287 and 288: Canvas Animations Subjects that we'
- Page 289 and 290: Canvas Animations We can set the co
- Page 291 and 292: Download from Wow! eBook Canvas An
- Page 293 and 294: Canvas Animations All imageData obj
- Page 295 and 296: Canvas Animations context.beginPath
- Page 297 and 298: Canvas Animations For the next part
- Page 299 and 300: Canvas Animations Time for action -
- Page 301 and 302: Canvas Animations 3. Save the new p
- Page 303 and 304: Canvas Animations Time for action -
- Page 305 and 306: Canvas Animations Once the rectangl
- Page 307 and 308: Canvas Animations We then define tw
- Page 309 and 310: Canvas Animations }, context.lineTo
- Page 311 and 312: Canvas Animations Creating a canvas
- Page 313 and 314: Canvas Animations Time for action -
- Page 315 and 316: Canvas Animations What just happene
- Page 317 and 318: Canvas Animations What just happene
- Page 319: Canvas Animations aliens.splice(x,
- Page 323 and 324: Download from Wow! eBook
- Page 325 and 326: Pop Quiz Answers Using fadeToggle()
- Page 327 and 328: Pop Quiz Answers Using slideToggle
- Page 329 and 330: Pop Quiz Answers Implementing the p
- Page 331 and 332: Pop Quiz Answers Animating the canv
- Page 333 and 334: show() or hide() method, triggering
- Page 335 and 336: cssHooks using 239 cssHooks feature
- Page 337 and 338: fading animations 20 flicker effect
- Page 339 and 340: proximity animations about 207 exte
- Page 341 and 342: scaleY 232 skew 232 skewX 232 skewY
- Page 343 and 344: jQuery 1.4 Reference Guide ISBN: 97
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 ]