Skip to main content
Participating Frequently
October 31, 2013
Question

How to make a stick man running when pressing a key?

  • October 31, 2013
  • 1 reply
  • 1536 views

I created a movieclip named stickman. In that, I created an animation by drawing a sequence of move in everyframe, so that stickman can run. Now what I want is that when I press a key, the stick man will run from left to right and when I release the key, it will stop. This is my code:

RunningMan.stop();

stage
.addEventListener(KeyboardEvent.KEY_DOWN,keypresseddown);
function keypresseddown(event:KeyboardEvent😞void
{

var key:uint = event.keyCode;
switch (key) {
  
case Keyboard.LEFT :
  
{
      
RunningMan.play();
      
RunningMan.x-=10;
      
RunningMan.scaleX=-1;
      

  
};
   
case Keyboard.RIGHT :
   
{
       
RunningMan.play();  //play animated run
       
RunningMan.x+=10;
       
RunningMan.scaleX=1;
       

   
};
   
default: RunningMan.stop();
  
}
}

However, when I pressed and held a key, it moved from left to right without animated run.

How can I fix it? Thanks in advance.


This topic has been closed for replies.

1 reply

Inspiring
October 31, 2013

case conditions need a break() statement at the end:

switch (key) {
  
case Keyboard.LEFT :
      
RunningMan.play();
      
RunningMan.x-=10;
      
RunningMan.scaleX=-1;
       break();

   
case Keyboard.RIGHT :
       
RunningMan.play();  //play animated run
       
RunningMan.x+=10;
       
RunningMan.scaleX=1;
        break();
   
default: RunningMan.stop();break();
  
}

chaocanhaAuthor
Participating Frequently
November 1, 2013

Thanks, but when I pressed a key, the man moved without animation.

Inspiring
November 4, 2013

Make sure that the animatuion (the running part) is triggered with Runningman.play().