14 Replies Latest reply on May 7, 2015 5:32 AM by kglad

    Timeline/Animation Code Problems (Simple Character Walking)

    Araly

      I'm trying to get a character's legs to move in the direction it goes when walking, but it's only working correctly in one direction (upwards). I used 8 Layers in my timeline, using key frames and classic tweens, but I wouldn't be shocked if that was my error.

       

      I'm new to Flash as a whole but I'm significantly more comfortable with coding than animation, and for the life of me I can't figure out why the character keeps glitching. The other animations appear to work when you first press the A,S, or D key, but they do a really fast shuffle and then the gotoAndPlay(frame number) seems like it just doesn't want to work; it just goes back to the frame that I assigned and then stops.

       

      Here's my code:

      package  {

       

        import flash.display.MovieClip;

        import flash.events.Event;

        import flash.events.KeyboardEvent;

        import flash.net.URLRequest;

       

        public class unicornWalkCode extends MovieClip{

       

       

        public function unicornWalkCode() {

        // constructor code

       

        var dir: String = "stop";

       

       

       

        var g:grass = new grass();

        addChild(g);

        g.x=275;

        g.y=200;

       

        var p:unicorn = new unicorn();

        addChild(p);

        p.x=400;

        p.y=200;

       

        //Functions

        addEventListener(Event.ENTER_FRAME, main);

        function main(e: Event): void {

        moveUnicorn();

        }

        //////

       

        //Move Unicorn (Walking Controls)

       

        stage.addEventListener(KeyboardEvent.KEY_DOWN, dFun);

        function dFun(e: KeyboardEvent): void {

       

       

        if (e.keyCode == 87) { // W

        dir = "up";

        p.gotoAndPlay(175);

        }

        if (e.keyCode == 65) { // A

        dir = "left";

        p.gotoAndPlay(125);

        }

        if (e.keyCode == 83) { // S

        dir = "down";

        p.gotoAndPlay(25);

        }

        if (e.keyCode == 68) {  //D

        dir = "right";

        p.gotoAndPlay(75);

        }

        }

       

       

        stage.addEventListener(KeyboardEvent.KEY_UP, uFun);

        function uFun(e: KeyboardEvent): void {

       

       

        if (e.keyCode == 87) { // W

        dir = "stop";

        p.gotoAndPlay(175);

        }

        if (e.keyCode == 65) { // A

        dir = "stop";

        p.gotoAndPlay(100);

        }

        if (e.keyCode == 83) { // S

        dir = "stop";

        p.gotoAndPlay(1);

        }

        if (e.keyCode == 68) { // D

        dir = "stop";

        p.gotoAndPlay(50);

        }

        }

       

        /////////////////////////////////////////////////////////////////////////////////Function s

       

        //Move Unicorn

        function moveUnicorn(): void {

        if (dir == "up" && p.y > 0) { //Facing BACK

        p.y -= 3;

        }

        if (dir == "left" && p.x > 0) {

        p.x -= 3;

        }

        if (dir == "down" && p.y < 400) {

        p.y += 3;

        }

        if (dir == "right" && p.x < 550) { //Facing FRONT

        p.x += 3;

        }

        }

       

       

        ///Last Three Braces

        }

       

       

        }

      }

      ***I double checked the frame numbers and they're all right, as well as the frame numbers in my gotoAndPlay commands. :/

       

      The answer is probably right in front of my face, but I can't seem to figure it out.

      I appreciate any suggestions!

        • 1. Re: Timeline/Animation Code Problems (Simple Character Walking)
          kglad Adobe Community Professional & MVP

          you have down and up key listeners that can conflict. and it's not expected that you would have key down and key up listeners that would do different things even if they didn't conflict.

           

          so, what's your intention using key down and key up listeners like that?

          • 2. Re: Timeline/Animation Code Problems (Simple Character Walking)
            Araly Level 1

            I'm trying to have it move only when a key is held down. We did it this way in my class and it seemed to work fine, but I think you're right about them conflicting. The only thing I added was the gotoAndPlay frames, but I don't see how that could mess up the code if the key up and down listeners didn't cause each other any issues before.

            • 3. Re: Timeline/Animation Code Problems (Simple Character Walking)
              kglad Adobe Community Professional & MVP

              comment out your keyup listeners and retest.

              • 4. Re: Timeline/Animation Code Problems (Simple Character Walking)
                Araly Level 1

                It's still giving me the same problem, it just stops playing the timeline when going in three of the four directions. It works perfectly when I'm holding S, but I see no difference in that code than I do with the others.

                • 5. Re: Timeline/Animation Code Problems (Simple Character Walking)
                  kglad Adobe Community Professional & MVP

                  you're holding down the keys????

                   

                  that's a problem if you want the timeline to advance because those gotoAndPlay statements are going to repeatedly executed.  ie, the timeline is NOT going to advance.

                  • 6. Re: Timeline/Animation Code Problems (Simple Character Walking)
                    Araly Level 1

                    Yeah, it was my problem when I first started too which is why I moved them away from the enter frame event... I don't know where else they could go or why they would only re-execute it for up left and right.

                    • 7. Re: Timeline/Animation Code Problems (Simple Character Walking)
                      kglad Adobe Community Professional & MVP

                      use the trace function to see what you're doing.

                       

                      holding down a key causes repeated triggering of the keydown listener and you don't want that.  ie, use a boolean so the goto is only triggered once.  reset that boolean in your keyup listener.

                      • 8. Re: Timeline/Animation Code Problems (Simple Character Walking)
                        Araly Level 1

                        I'm at a complete loss. I added traces to each direction (in dFun) and it only calls up and right once(the two directions that are now working), whereas the others are called every frame. The code is exactly the same, and when I switched the order it did nothing.

                         

                        Won't a boolean still have the same issue since it's true the entire time the key is held down?

                        • 9. Re: Timeline/Animation Code Problems (Simple Character Walking)
                          Araly Level 1

                          Okay, I guess Flash is out to get me today. I changed the key codes just to see if that would impact anything: instead of having S, I changed it to X and now the walking downwards animation is perfect, but upon changing A to Z, it still glitches.

                          • 10. Re: Timeline/Animation Code Problems (Simple Character Walking)
                            kglad Adobe Community Professional & MVP

                            i don't know what you're seeing but dFun is called at the keyboard repeat rate when any key is held down.

                             

                            plus, it looks like you have everything within your constructor and that's probelmatic.  those anonymous functions should not be nested in your constructor.

                            • 11. Re: Timeline/Animation Code Problems (Simple Character Walking)
                              Araly Level 1

                              Again I'm really new to flash, so sorry if some of my code is way off what it should be haha. I agree that when a key is held down the code should be called repeatedly, but for some reason it's only doing that for some of my keys.

                               

                              I managed to change WASD to keys that have the character walk smoothly, but I may eventually convert it to mouse events with arrow controls on the stage.. it wouldn't be practical to reach across the keyboard just to have the avatar walk.

                               

                              I don't really know why certain keys work and others don't, but thank you for your help. One more question though, why does the code being under //constructor code matter if it's just a comment?

                              • 12. Re: Timeline/Animation Code Problems (Simple Character Walking)
                                kglad Adobe Community Professional & MVP

                                it doesn't matter where you place comments.  it's the rest of your code that's problematic.

                                 

                                you should have something like

                                package  {

                                 

                                  import flash.display.MovieClip;

                                  import flash.events.Event;

                                  import flash.events.KeyboardEvent;

                                  import flash.net.URLRequest;

                                 

                                  public class unicornWalkCode extends MovieClip{

                                 

                                  var dir: String = "stop";

                                 

                                  var g:grass = new grass();

                                  var p:unicorn = new unicorn();

                                var keydownBool:Boolean;

                                 

                                  public function unicornWalkCode() {

                                  // constructor code

                                  addChild(g);

                                  g.x=275;

                                  g.y=200;

                                 

                                  addChild(p);

                                  p.x=400;

                                  p.y=200;

                                 

                                  //Functions

                                  addEventListener(Event.ENTER_FRAME, main);

                                 

                                 

                                  stage.addEventListener(KeyboardEvent.KEY_DOWN, dFun);

                                 

                                  stage.addEventListener(KeyboardEvent.KEY_UP, uFun);

                                }

                                 

                                  function main(e: Event): void {

                                  moveUnicorn();

                                  }

                                  //////

                                 

                                  //Move Unicorn (Walking Controls)


                                  function dFun(e: KeyboardEvent): void {

                                if(keydown){

                                return;

                                }

                                keydown=true;

                                  if (e.keyCode == 87) { // W

                                  dir = "up";

                                  p.gotoAndPlay(175);

                                  }

                                  if (e.keyCode == 65) { // A

                                  dir = "left";

                                  p.gotoAndPlay(125);

                                  }

                                  if (e.keyCode == 83) { // S

                                  dir = "down";

                                  p.gotoAndPlay(25);

                                  }

                                  if (e.keyCode == 68) {  //D

                                  dir = "right";

                                  p.gotoAndPlay(75);

                                  }

                                  }

                                 

                                  function uFun(e: KeyboardEvent): void {

                                keydown=false;

                                  if (e.keyCode == 87) { // W

                                  dir = "stop";

                                  p.gotoAndPlay(175);

                                  }

                                  if (e.keyCode == 65) { // A

                                  dir = "stop";

                                  p.gotoAndPlay(100);

                                  }

                                  if (e.keyCode == 83) { // S

                                  dir = "stop";

                                  p.gotoAndPlay(1);

                                  }

                                  if (e.keyCode == 68) { // D

                                  dir = "stop";

                                  p.gotoAndPlay(50);

                                  }

                                  }

                                 

                                  /////////////////////////////////////////////////////////////////////////////////Function s

                                 

                                  //Move Unicorn

                                  function moveUnicorn(): void {

                                  if (dir == "up" && p.y > 0) { //Facing BACK

                                  p.y -= 3;

                                  }

                                  if (dir == "left" && p.x > 0) {

                                  p.x -= 3;

                                  }

                                  if (dir == "down" && p.y < 400) {

                                  p.y += 3;

                                  }

                                  if (dir == "right" && p.x < 550) { //Facing FRONT

                                  p.x += 3;

                                  }

                                  }

                                 

                                 

                                  }

                                }

                                • 13. Re: Timeline/Animation Code Problems (Simple Character Walking)
                                  Araly Level 1

                                  Ah, alright, I was worried there was some new formatting concept that we hadn't learned. I just tend to get lazy when I do short code.. hence the scattered mess up there. xD Thanks for your help again!