@@ -533,12 +533,10 @@ copied, and the sixth to ninth argument give the rectangle (on the
533
533
canvas) into which it should be copied.
534
534
535
535
This can be used to pack multiple _sprites_ (image elements) into a
536
- single image file, and then copy out the part you need. For example,
536
+ single image file, and then only draw the part you need. For example,
537
537
we have this picture containing a game character in multiple poses.
538
538
539
- // FIXME update with new art
540
-
541
- image::img/player.png[alt="Various poses of a game character"]
539
+ image::img/player_big.png[alt="Various poses of a game character"]
542
540
543
541
If we alternate which pose we draw, we can show an animation that
544
542
looks like a walking character.
@@ -547,7 +545,7 @@ To animate the picture on a canvas, the `clearRect` method is useful.
547
545
It resembles `fillRect`, but instead of coloring the rectangle, it
548
546
resets it back to transparent.
549
547
550
- We know that each _sprite_, each sub-picture, is 16 pixels wide and 30
548
+ We know that each _sprite_, each sub-picture, is 24 pixels wide and 30
551
549
pixels high. The code below loads the image, and then sets up an
552
550
interval (repeated timer) to draw the next frame.
553
551
@@ -558,7 +556,7 @@ interval (repeated timer) to draw the next frame.
558
556
var cx = document.querySelector("canvas").getContext("2d");
559
557
var img = document.createElement("img");
560
558
img.src = "img/player.png";
561
- var spriteW = 16 , spriteH = 30;
559
+ var spriteW = 24 , spriteH = 30;
562
560
img.addEventListener("load", function() {
563
561
var cycle = 0;
564
562
setInterval(function() {
@@ -684,7 +682,7 @@ the world around the character's vertical center:
684
682
var cx = document.querySelector("canvas").getContext("2d");
685
683
var img = document.createElement("img");
686
684
img.src = "img/player.png";
687
- var spriteW = 16 , spriteH = 30;
685
+ var spriteW = 24 , spriteH = 30;
688
686
img.addEventListener("load", function() {
689
687
flipHorizontally(cx, 100 + spriteW / 2);
690
688
cx.drawImage(img, 0, 0, spriteW, spriteH,
@@ -932,7 +930,7 @@ Tiles that are not empty (null) are drawn with `drawImage`. The
932
930
are not the player. It contains, from left to right, the wall tile,
933
931
the lava tile, and then the sprite for a coin.
934
932
935
- image::img/sprites_background .png[alt="Sprites for our game"]
933
+ image::img/sprites_big .png[alt="Sprites for our game"]
936
934
937
935
Background tiles are 20 by 20 pixels, since we will use the same scale
938
936
that we used in `DOMDisplay`. Thus, the offset for lava tiles is 20
@@ -947,13 +945,18 @@ scene will appear as soon as the loading finishes.
947
945
948
946
The walking character we used before will be used to represent the
949
947
player. The code that draws it needs to pick the right sprite and
950
- direction based on the player's current motion. When the player is
951
- standing still, we draw the leftmost sprite. During jumps (when the
952
- vertical speed is not zero), we use the rightmost sprite. When
953
- walking, we cycle through the first 8 sprites based on the display's
954
- `animationTime` property. This is measured in seconds, and we want to
955
- switch frames twelve times per second, so the time is multiplied by 12
956
- first.
948
+ direction based on the player's current motion. The first 8 sprites
949
+ contain a walking animation. When the player is moving along a floor,
950
+ we cycle through them based on the display's `animationTime` property.
951
+ This is measured in seconds, and we want to switch frames twelve times
952
+ per second, so the time is multiplied by 12 first. When the player is
953
+ standing still, we draw the ninth sprite. During jumps (when the
954
+ vertical speed is not zero), we use the tenth, rightmost sprite.
955
+
956
+ Because the sprites are slightly wider than the player object—24
957
+ instead of 16 pixels, to allow some space for feet and arms—the method
958
+ has to adjust the x coordinate and width by a given amount
959
+ (`playerXOverlap`).
957
960
958
961
// include_code
959
962
@@ -962,15 +965,18 @@ first.
962
965
----
963
966
var playerSprites = document.createElement("img");
964
967
playerSprites.src = "img/player.png";
968
+ var playerXOverlap = 4;
965
969
966
970
CanvasDisplay.prototype.drawPlayer = function(x, y, width,
967
971
height) {
968
- var sprite = 0, player = this.level.player;
972
+ var sprite = 8, player = this.level.player;
973
+ width += playerXOverlap * 2;
974
+ x -= playerXOverlap;
969
975
if (player.speed.x)
970
976
this.flipPlayer = player.speed.x < 0;
971
977
972
978
if (player.speed.y)
973
- sprite = 8 ;
979
+ sprite = 9 ;
974
980
else if (player.speed.x)
975
981
sprite = Math.floor(this.animationTime * 12) % 8;
976
982
0 commit comments