Adding sprites to the game framework

Adding sprites to the game framework

We will use the animated woman example and take the sprite utility functions and some predefined values, such as the sprite sheet URL, the size of the sprites, the number of postures, etc., and add it all to one of the examples that used the game framework (the last one from the time based animation lesson (to keep things simple, we did not use the ones with gamepad, etc)).

First, try this example at JsBin

The woman sprite in the game framework, jsbin screenshot

how to add sprites to the game framework…

    1. We declare a woman object, similar to the monster object, with x, y, speed, width properties. We add a direction property that corresponds to a posture’s index (so direction = 2 corresponds to the sprite animation for the woman moving to the left, whereas direction = 6 corresponds to the posture of the woman moving to the right…)
    2. We add the Sprite and SpriteImage objects to the game framework,
    3. We write a loadAssets(callback) function which: a) loads the sprite sheet, b) extracts all the woman sprites and builds the womanSprites array, and c) calls the callback function passed as a parameter once finished,
    4. We call the loadAssets function from the game framework start function, and we start the animation loop only when the loadAssetsfunction has completed loading and extracting the sprites. In a real game the loadAssets function would also load the sounds, and perhaps other sprite sheets or resources etc. In this function you could use the BufferLoader utility for loading multiple resources asynchronously, as  discussed during week 1.

Source code extract:

  1. // Inits
  2. window.onload = function init() {
  3. var game = new GF();
  4. game.start();
  5. };
  6. // GAME FRAMEWORK STARTS HERE
  7. var GF = function(){
  8.    
  9.    // Woman object and sprites
  10.    // sprite index corresponding to posture
  11.    var WOMAN_DIR_RIGHT = 6;
  12.    var WOMAN_DIR_LEFT = 2;
  13.    var woman = {
  14.       x:100,
  15.       y:200,
  16.       width:48,
  17.       speed:100, // pixels/s this time!
  18.       direction: WOMAN_DIR_RIGHT
  19.    };
  20.    var womanSprites = [];
  21.    var mainLoop = function(time){
  22.      
  23.      // Draw a woman moving left and right
  24.     womanSprites[woman.direction].draw(ctx, woman.x, woman.y);
  25.     updateWomanPosition(delta);
  26.     
  27.   };
  28.   function updateWomanPosition(delta) {
  29.     // check collision on left or right
  30.     if(((woman.x+woman.width) > canvas.width) || (woman.x < 0)) {
  31.       // inverse speed
  32.       woman.speed = woman.speed;
  33.     }
  34.     // change sprite direction
  35.     if(woman.speed >= 0) {
  36.       woman.direction = WOMAN_DIR_RIGHT;
  37.     } else {
  38.       woman.direction = WOMAN_DIR_LEFT;
  39.     }
  40.     woman.x += calcDistanceToMove(delta, woman.speed);
  41.   }
  42.   /*—————————————*/
  43.   /* SPRITE UTILITY FUNCTIONS              */
  44.   /*—————————————*/
  45.   function SpriteImage(img, x, y, width, height) {
  46.     
  47.     this.draw = function(ctx, xPos, yPos, scale) {…};
  48.   }
  49.   function Sprite() {
  50.     
  51.     this.extractSprites = function(…) {…};
  52.     this.drawStopped = function(ctx, x, y) {…};
  53.     this.draw = function(ctx, x, y) {…};
  54.     this.setNbImagesPerSecond = function(nb) {…};
  55.   }
  56.   /*—————————————*/
  57.   /* EN OF SPRITE UTILITY FUNCTIONS        */
  58.   /*—————————————*/
  59. var loadAssets = function(callback) {
  60.     var SPRITESHEET_URL = http://i.imgur.com/3VesWqx.png&#8221;;
  61.     var SPRITE_WIDTH = 48;
  62.     var SPRITE_HEIGHT = 92;
  63.     var NB_POSTURES=8;
  64.     var NB_FRAMES_PER_POSTURE = 13;
  65.     // load the spritesheet
  66.     var spritesheet = new Image();
  67.     spritesheet.src = SPRITESHEET_URL;
  68.     // Called when the spritesheet has been loaded
  69.     spritesheet.onload = function() {
  70.       // Create woman sprites
  71.       for(var i = 0; i < NB_POSTURES; i++) {
  72.         var sprite = new Sprite();
  73.         sprite.extractSprites(spritesheet, NB_POSTURES, (i+1),
  74.                               NB_FRAMES_PER_POSTURE,
  75.                               SPRITE_WIDTH, SPRITE_HEIGHT);
  76.         sprite.setNbImagesPerSecond(20);
  77.         womanSprites[i] = sprite;
  78.       }
  79.       // call the callback function passed as a parameter,
  80.       // we’re done with loading assets and building the sprites
  81.       callback();
  82.     };
  83.   };
  84.   var start = function(){
  85.     
  86.     // Load sounds and images, then when this is done, start the mainLoop
  87.     loadAssets(function() {
  88.        // We enter here only when all assets have been loaded
  89.        requestAnimationFrame(mainLoop);
  90.     });
  91.   };
  92.   
  93.  };

Leave a comment