@@ -6,7 +6,7 @@ export default (spritesheet, numCol, numRow, delay) => {
66
77 let currentFrame = 0 , then ;
88
9- let currentAnimation , prevAnimation ;
9+ let currentAnimation = { frameStart : 0 } , prevAnimation ;
1010
1111 return {
1212
@@ -24,7 +24,7 @@ export default (spritesheet, numCol, numRow, delay) => {
2424
2525 } ,
2626
27- draw ( ctx , x , y , w , h , flipped = false ) {
27+ draw ( ctx , x , y , w = frameWidth , h = frameHeight , flippedX = false , flippedY = false ) {
2828
2929 const now = performance . now ( ) ;
3030
@@ -45,25 +45,25 @@ export default (spritesheet, numCol, numRow, delay) => {
4545 }
4646
4747 let col = currentFrame % numCol , row = Math . floor ( currentFrame / numCol ) ;
48-
48+
4949 ctx . save ( ) ;
5050
51- if ( flipped ) { // flips the sprite horizontally
51+ if ( flippedX || flippedY ) {
5252
5353 ctx . translate ( x + w / 2 , y + w / 2 ) ;
5454
55- ctx . scale ( - 1 , 1 ) ;
56-
57- ctx . translate ( - ( x + w / 2 ) , - ( y + w / 2 ) ) ;
55+ ctx . scale ( flippedX ? - 1 : 1 , flippedY ? - 1 : 1 ) ;
5856
57+ ctx . translate ( - ( x + w / 2 ) , - ( y + w / 2 ) ) ;
58+
5959 }
6060
6161 ctx . drawImage ( spritesheet , col * frameWidth , row * frameHeight , frameWidth , frameHeight , x , y , w , h ) ;
62-
62+
6363 ctx . restore ( ) ;
6464
6565 }
6666
6767 }
6868
69- } ;
69+ } ;
0 commit comments