7 Replies Latest reply on Aug 9, 2010 10:34 PM by kglad

    using arrow keys to change an object's facing

    Shad0wdrag0n Level 1

      I have a MC that can be moved around with the arrow keys. When a person presses the left key I want the object to face left and perform an animation for as long as the left key is pressed. When the left key is released I want the animation to go back to it's starting frame but the MC should still be facing left. I want the same thing for the other arrow keys, but the animations will be different for each direction (for example: walk right, walk left, walk away from you, walk towards you). What's the best way to do something like this?

        • 1. Re: using arrow keys to change an object's facing
          kglad Adobe Community Professional & MVP

          for left/right, changing the _xscale to 100 and -100 usually works.  how the up/down should work depends on the movieclip.

          • 2. Re: using arrow keys to change an object's facing
            Shad0wdrag0n Level 1

            For directions imagine a character moving around. I'll have three animations, a side view of the character walking (for left and right movement), a rear view of the character walking (for moving up), and a front view of the character walking (for moving down). I can get the mc to move around, and I can get it to start the proper animation for the direction the character is moving in, but I can't get it to go to the proper standing still frame when the key is released so the character is facing in the proper direction (left if the left key is released, back/up if the up key is released, etc).

            • 3. Re: using arrow keys to change an object's facing
              kglad Adobe Community Professional & MVP

              why doesn't a gotoAndStop() work for you?

              • 4. Re: using arrow keys to change an object's facing
                Shad0wdrag0n Level 1

                I thought about something like the code below, but I don't know how to make it so releasing a specific key makes the mc go to a specific frame. I also don't know what overlapping keys will do with such a code (like when you hold down the up key, then press the right key to go diagonally, then release both keys).

                 

                if(Key.isDown(Key.LEFT)){

                player._x -= speed;

                player._xscale = -100;

                if(player._currentframe==1){ 

                player.gotoAndPlay(2);

                }

                }

                if(Key.isDown(Key.RIGHT)){

                player._x += speed;

                player._xscale = 100;

                if(player._currentframe==1){ 

                player.gotoAndPlay(2);

                }

                }

                if(Key.isDown(Key.UP)){

                player._y -= speed;

                if(player._currentframe==1){ 

                player.gotoAndPlay(22);

                }

                }

                if(Key.isDown(Key.DOWN)){

                player._y += speed;

                if(player._currentframe==1){ 

                player.gotoAndPlay(33);

                }

                }

                if (!Key.isDown(Key.DOWN) && !Key.isDown(Key.UP) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {

                player.gotoAndStop (1);

                }

                • 5. Re: using arrow keys to change an object's facing
                  kglad Adobe Community Professional & MVP

                  use something like:

                   

                  var dir:String;

                   

                  if(Key.isDown(Key.LEFT)){

                  dir="left";

                  player._x -= speed;

                  player._xscale = -100;

                  if(player._currentframe==1){ 

                  player.gotoAndPlay(2);

                  }

                  }

                  if(Key.isDown(Key.RIGHT)){

                  dir="right";

                  player._x += speed;

                  player._xscale = 100;

                  if(player._currentframe==1){ 

                  player.gotoAndPlay(2);

                  }

                  }

                  if(Key.isDown(Key.UP)){

                  dir="up";

                  player._y -= speed;

                  if(player._currentframe==1){ 

                  player.gotoAndPlay(22);

                  }

                  }

                  if(Key.isDown(Key.DOWN)){

                  dir="down";

                  player._y += speed;

                  if(player._currentframe==1){ 

                  player.gotoAndPlay(33);

                  }

                  }

                  if (!Key.isDown(Key.DOWN) && !Key.isDown(Key.UP) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {

                  player.gotoAndStop (dir);

                  }

                  • 6. Re: using arrow keys to change an object's facing
                    Shad0wdrag0n Level 1

                    Ah, yes, that should work. Thanks.