Advertisement
GoToBread

JS Sprite Sheet Animation Class

Jan 30th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //SpriteSheet for handling walk animations.
  2. //Based off: http://gamedevelopment.tutsplus.com/tutorials/an-introduction-to-spritesheet-animation--gamedev-13099
  3.  
  4. function SpriteSheet(path, frameWidth, frameHeight) {
  5.     this.image = new Image();
  6.     this.frameWidth  = frameWidth;
  7.     this.frameHeight = frameHeight;
  8.     var self = this;
  9.    
  10.     // calculate the number of frames in a row after the image loads
  11.     this.image.onload = function() {
  12.         self.framesPerRow = Math.floor(self.image.width / self.frameWidth);
  13.     };
  14.    
  15.     this.image.src = path;
  16. }
  17.  
  18.  
  19. function Animation(spritesheet, startFrame, endFrame, frameSpeed) {
  20.     var animationSequence = [];  // array holding the order of the animation
  21.     this.currentFrame = 0;       // the current frame to draw
  22.     var counter = 0;             // keep track of frame rate
  23.     var self = this;
  24.    
  25.     // create the sequence of frame numbers for the animation.
  26.     for (var frameNumber = startFrame; frameNumber <= endFrame; frameNumber++) {
  27.         animationSequence.push(frameNumber);
  28.     }
  29.    
  30.     this.updateAnimation = function() {
  31.         // update to the next frame if it is time
  32.         if (counter == (frameSpeed - 1)) {
  33.             self.currentFrame = (self.currentFrame + 1) % animationSequence.length;
  34.         }
  35.         counter = (counter + 1) % frameSpeed;
  36.     };
  37.    
  38.     this.draw = function(ctx, x, y) {
  39.         x = x || 0;
  40.         y = y || 0;
  41.        
  42.         // get the row and col of the frame
  43.         var col = Math.floor(animationSequence[self.currentFrame] % spritesheet.framesPerRow);
  44.         var row = Math.floor(animationSequence[self.currentFrame] / spritesheet.framesPerRow);
  45.        
  46.         ctx.drawImage(
  47.             spritesheet.image,
  48.             col * spritesheet.frameWidth,
  49.             row * spritesheet.frameHeight,
  50.             spritesheet.frameWidth, spritesheet.frameHeight,
  51.             x, y,
  52.             spritesheet.frameWidth, spritesheet.frameHeight);
  53.     };
  54. }
  55.  
  56. /*
  57. These classes work by creating a SpriteSheet object and one
  58. Animation object for every animation you want your entity to have.
  59.  
  60. var player = {
  61.     x: 0, y: 0,
  62.     sprite: new SpriteSheet("foo.png", 12, 20),
  63.     attack: null //attack references player.sprite and so can't be initialized here.
  64. };
  65.  
  66. player.attack = new Animation(player.sprite, 0, 5, 4);
  67.  
  68. To use this animation, call updateAnimation() in your
  69. drawing loop. This will loop the animation over and over.
  70. If you don't want to loop, but simply to draw one frame,
  71. set currentFrame to whatever, and call the Animation object's
  72. draw() method without calling updateAnimation():
  73.  
  74. draw() {
  75.     player.attack.draw(ctx, player.x, player.y);
  76.     if(player.animating) {
  77.         player.attack.updateAnimation();
  78.     }
  79. }
  80.  
  81. All handling of different animations, such as separate strips
  82. of the same spritesheet, is done in the main code as opposed
  83. to the object. This gives the object less usability, but gives
  84. the developer more freedom to use it how s/he wants.
  85.  
  86. All this class does is animate over a set of frames.
  87. That is its only intended purpose.
  88. Anything else must be made yourself for the time being.
  89. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement