11 Replies Latest reply: Apr 24, 2012 7:33 AM by WitchXHunter RSS

    [AS2] Character movement

    xmasgame Community Member

      Okey my character got this script: (its lots more but yeah.. this is the trouble code).

       

      Frame 1 = character stand still

      Frame 2 = character (MC) - movement

      Frame 3 = character (MC) - Jump movement

       

      It works fine, when I press UP, he jumpes and play frame 3.

      When i press left and right it plays frame 2.

      When I press .. nothing, frame 1 is played.

      That correct!

      BUT when i press .. left + up, or right + up, it will play frame 1.

      Any idea how i can make it play Frame 3 (jump) even if I press two buttons at the same time? Would be awesome if someone knew it.

       

      onClipEvent (enterFrame) {

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

      _root.player.gotoAndStop(2);

      }

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

      _root.player.gotoAndStop(2);

      }

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

      _root.player.gotoAndStop(3);

      }

      if (!Key.isDown(Key.LEFT) and (!Key.isDown(Key.RIGHT) and (!Key.isDown(Key.UP)))) {

      this.gotoAndStop(1);

      }

      }

        • 1. Re: [AS2] Character movement
          WitchXHunter

          onClipEvent (enterFrame) {

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

           

           

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

                     do something

           

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

                      do something

           

                              }

                              _root.player.gotoAndStop(3);

                    } else {

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

                                        _root.player.gotoAndStop(2);

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

                                        _root.player.gotoAndStop(2);

                              } else {

                                        this.gotoAndStop(1);

                              }

                    }

          }

           

          This should solve that. but I can't guarantee if it will cause any problem for your game.

          • 2. Re: [AS2] Character movement
            xmasgame Community Member

            That gave alot of errors hmm, to tired to check it out tonight. But what do you mean with "do something" ?

            • 3. Re: [AS2] Character movement
              prodigalmaster Community Member

              Just use the and opperator: &&

               

              so something like:

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

              _root.player.gotoAndStop(3);

              }

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

              _root.player.gotoAndStop(3);

              }


              • 4. Re: [AS2] Character movement
                WitchXHunter Community Member

                the do something is not surpose to be in the code, it means the place for you to add your actions inside. like setting a different movement speed where it's nested without having to assign the jumping again.  prodigal master's code will work as well. depending on what way you want to script it.

                • 5. Re: [AS2] Character movement
                  xmasgame Community Member

                  Prodigalmaster, it did sound abit to easy with that code and it was, it did not work .. hmm

                  The issue is that whatever I do, when i press MORE buttons, the character FREEZE on frame 3 and wont play the MC

                  So in overall the code is right, it is going to frame 3 .. BUT it wont play the MC.

                  If i only press UP btw, it will play the MC

                  • 6. Re: [AS2] Character movement
                    prodigalmaster Community Member

                    Maybe that is because it is repeatedly goto and stopping on frame (3) due to the key's being down. Maybe use a boolean variable e.g. isJumping = false; and when the jump animationbegins make it isJumping = true; and finally when the animation ends make isJumping = false; and therefore in the code use && in the if statement with isJumping == false

                    • 7. Re: [AS2] Character movement
                      xmasgame Community Member

                      Not that good with booleans I  have to admit.. anyhow I did put "play" in the first frame of the MC in frame 3, that did not work out either.

                      • 8. Re: [AS2] Character movement
                        prodigalmaster Community Member

                        A boolean is just the name for a variable with two states. In flash this is true and false.

                        Just try what I said.

                         

                        You know how to make a variable right?

                        var isJumping:Boolean = false;

                        (on the main timeline).

                         

                        So, there are 3 frames in the player movieclip, and animation movieclips inside those frames of the player?

                         

                        Did you try WitchXHunters' code without the 'do something' bit?

                        • 9. Re: [AS2] Character movement
                          xmasgame Community Member

                          It made lots of errors but when I removed some scripts and removed do something it Worked thx for the head up.

                          Love you guys ^^

                          • 10. Re: [AS2] Character movement
                            xmasgame Community Member

                            Epic Script, thx again. My bad at the start, I had some code I had to remove to make that work and such.. When I read your script again today, it made it clear why it gave errors

                            • 11. Re: [AS2] Character movement
                              WitchXHunter Community Member

                              you're welcome .^^