Sprite animation not looping

14 views
Skip to first unread message

benjamin dudok

unread,
Oct 20, 2012, 10:56:56 AM10/20/12
to gam...@googlegroups.com
I'm probably overlooking something, but i can't get my sprite animations to loop in the script below. I got 2 animations, walking and standing. And i'm trying to switch between the two when i press a button. the switching works, but if i hold down the button, the walking animation doesn't loop. I used certain code snippets from the Telemachus example and the animator.js is from the sprite and animations tutorial.

// create player sprites and put them in a group
    var playerSheet = new animator.SpriteSheet('images/player.gif',  {width: 32, height: 44});
    var playerAnimation = new animator.AnimationSheet(playerSheet, {'idle': [0], 'walk': [1,12]}, 25);
    var playercharacter = new PlayerCharacter([50, 400], playerAnimation);
    playercharacter.moveDelta = [0, 0];
    playercharacter.moveSpeed = 10;
    playerAnimation.start('idle');
     
   
    var keysDown = {};
    function handleEvent(event) {
      if (event.type === gamejs.event.KEY_DOWN) {
         keysDown[event.key] = true;
      } else if (event.type === gamejs.event.KEY_UP) {
         keysDown[event.key] = false;
      };
      
      // left, right
      if (keysDown[gamejs.event.K_a]) {
         playercharacter.moveDelta[0] = -playercharacter.moveSpeed;
         playerAnimation.start('walk');
      } else if (keysDown[gamejs.event.K_d]) {
         playercharacter.moveDelta[0] = +playercharacter.moveSpeed;
         playerAnimation.start('walk');
      } else {
         playercharacter.moveDelta[0] = 0;
         playerAnimation.start('idle');
      }

      // up / down
      if (keysDown[gamejs.event.K_w]) {
         playercharacter.moveDelta[1] = -playercharacter.moveSpeed
         playerAnimation.start('walk');
      } else if (keysDown[gamejs.event.K_s]) {
         playercharacter.moveDelta[1] = +playercharacter.moveSpeed;
         playerAnimation.start('walk');
      }  else {
         playercharacter.moveDelta[1] = 0;
      }
   };

Simon Oberhammer

unread,
Oct 23, 2012, 5:51:04 AM10/23/12
to gam...@googlegroups.com
Hi Benjamin,
from the code it seem you keep (re-)starting the the animation the whole time while a key is down - this won't work; you'll have to keep track of whether the right animation is already running and only start it when needed.

hth
 simon
Reply all
Reply to author
Forward
0 new messages