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

Time for action – adding handlers to control the ship The following block of code should be added to the onload event handler for the ship image object: ship.onload = function () { context.drawImage(ship, shipPos[0], shipPos[1]); addAliens(); $(canvas).focus().bind("keydown", function (e) { if (e.which === 37 || e.which === 39) { context.clearRect(shipPos[0], shipPos[1], ship.width, ship.height); [ 297 ] Chapter 10 if (e.which === 37 && shipPos[0] > 4) { shipPos[0] = shipPos[0] - 4; } else if (e.which === 39 && shipPos[0] < 896 - ship.width) { shipPos[0] = shipPos[0] + 4; } context.drawImage(ship, shipPos[0], shipPos[1]); } else if (e.which === 32) { context.fillStyle = "#fff"; var bulletPos = shipPos[0] + 20, newBulletPos = [bulletPos, 596], alienLength = aliens.length, fire = function () { if (newBulletPos[1] > 0) { context.clearRect(newBulletPos[0], newBulletPos[1], 3, 6); newBulletPos[1] = newBulletPos[1] - 2; context.fillRect(newBulletPos[0], newBulletPos[1], 3, 6); for (var x = 0; x < alienLength; x++) { if (newBulletPos[1] === aliens[x].posY || newBulletPos[1] === aliens[x].posY + aliens[x].img.height) { if (newBulletPos[0] > aliens[x].posX && newBulletPos[0] - aliens[x].posX < aliens[x].img.width + 13) { context.clearRect(aliens[x].posX, aliens[x].posY, aliens[x].img.width, aliens[x].img.height);

Canvas Animations aliens.splice(x, 1); clearInterval(bulletInt); context.clearRect(newBulletPos[0], newBulletPos[1], 3, 6); if (!aliens.length) { clearInterval(motionInt); dirCounter = 0; alienSpeed = alienSpeed - 100; addAliens(); } } } } } else { context.clearRect(newBulletPos[0], newBulletPos[1], 3, 6); clearInterval(bulletInt); } }, bulletInt = setInterval(function () { fire(); }, 1); } }); }; What just happened? We use jQuery to attach an event handler to the element that listens for keydown events. Although we're not providing support for IE and so don't need jQuery for its cross-browser normalization when attaching events, it still makes the event handling process much easier. Within the function that is executed whenever a keydown event is detected, we check for the presence of either the left or right arrow keys, which have a which property in the event object of 37 and 39, or the space bar, which has the code 32. If the code 37 or 39 is detected we then use a nested if statement to determine between the two keys. We also check that the ship hasn't reached either the left edge, or the right edge of the canvas. We then use the clearRect() function to remove the ship and draw a new one either 4 pixels to the left, or 4 pixels to the right depending on which key was pressed. This gives the ship left and right motion along the bottom of the canvas. The second branch of the outer conditional deals with the space bar being pressed, which causes a bullet to leave the ship and travel in a straight line to the top of the canvas. The bullets will be white, so we set the fillStyle property of the canvas to #fff. [ 298 ]

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

aliens.splice(x, 1);<br />

clearInterval(bulletInt);<br />

context.clearRect(newBulletPos[0],<br />

newBulletPos[1], 3, 6);<br />

if (!aliens.length) {<br />

clearInterval(motionInt);<br />

dirCounter = 0;<br />

alienSpeed = alienSpeed - 100;<br />

addAliens();<br />

}<br />

}<br />

}<br />

}<br />

} else {<br />

context.clearRect(newBulletPos[0], newBulletPos[1], 3, 6);<br />

clearInterval(bulletInt);<br />

}<br />

},<br />

bulletInt = setInterval(function () { fire(); }, 1);<br />

}<br />

});<br />

};<br />

What just happened?<br />

We use <strong>jQuery</strong> to attach an event handler to the element that listens for<br />

keydown events. Although we're not providing support for IE and so don't need <strong>jQuery</strong> for<br />

its cross-browser normalization when attaching events, it still makes the event handling<br />

process much easier.<br />

Within the function that is executed whenever a keydown event is detected, we check for<br />

the presence <strong>of</strong> either the left or right arrow keys, which have a which property in the event<br />

object <strong>of</strong> 37 and 39, or the space bar, which has the code 32.<br />

If the code 37 or 39 is detected we then use a nested if statement to determine between<br />

the two keys. We also check that the ship hasn't reached either the left edge, or the right<br />

edge <strong>of</strong> the canvas.<br />

We then use the clearRect() function to remove the ship and draw a new one either 4<br />

pixels to the left, or 4 pixels to the right depending on which key was pressed. This gives the<br />

ship left and right motion along the bottom <strong>of</strong> the canvas.<br />

The second branch <strong>of</strong> the outer conditional deals with the space bar being pressed, which<br />

causes a bullet to leave the ship and travel in a straight line to the top <strong>of</strong> the canvas. The<br />

bullets will be white, so we set the fillStyle property <strong>of</strong> the canvas to #fff.<br />

[ 298 ]

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

Saved successfully!

Ooh no, something went wrong!