5 Replies Latest reply on Jun 15, 2010 4:27 PM by kglad

    I need better code for moving an object

    Shad0wdrag0n Level 1

      I'm currently using an older code to move objects around where the code is on the object being moved. I'd like to swap it out for code that isn't on the object, but I'm not too sure what it should be. Anyway, this is what I have right now:

       

      _global.speed=10

      onClipEvent(enterFrame){

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

                this._x -= speed;

                if(this._currentframe==1){

                     this.gotoAndPlay("walkLeft");

                }

           }

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

                this._x += speed;

                if(this._currentframe==1){

                     this.gotoAndPlay("walkRight");

                }

           }

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

                this._y -= speed;

                if(this._currentframe==1){

                     this.gotoAndPlay("walkUp");

                }

           }

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

                this._y += speed;

                if(this._currentframe==1){

                     this.gotoAndPlay("walkDown");

                }

           }

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

                this.gotoAndStop (1);

           }

           if(_parent.border.hitTest(_x+(_width/2),_y,true)){

                this._x -= speed;

           }

           if(_parent.border.hitTest(_x-(_width/2),_y,true)){

                this._x += speed;

           }

           if(_parent.border.hitTest(_x,_y+(_height/2),true)){

                this._y -= speed;

           }

           if(_parent.border.hitTest(_x,_y-(_height/2),true)){

                this._y += speed;

           }

      }

       

      So, how would I move this off the object and onto a frame? Is there a better way to handle the hitTest? How do I make it so that the object keeps facing in the direction it was moving when you stop pressing the arrow key?

        • 1. Re: I need better code for moving an object
          kglad Adobe Community Professional & MVP

          use something like the following where mc is your person movieclip:

           

          var speed:Number=10;


          this.onEnterFrame=loopF;


          function loopF() {

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

                  mc._x -= speed;

                  if (mc._currentframe == 1) {

                      mc.gotoAndPlay("walkLeft");

                  }

              }

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

                  mc._x += speed;

                  if (mc._currentframe == 1) {

                      mc.gotoAndPlay("walkRight");

                  }

              }

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

                  mc._y -= speed;

                  if (mc._currentframe == 1) {

                      mc.gotoAndPlay("walkUp");

                  }

              }

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

                  mc._y += speed;

                  if (mc._currentframe == 1) {

                      mc.gotoAndPlay("walkDown");

                  }

              }

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

                  mc.gotoAndStop(1);

              }

              if (border.hitTest(mc._x+(mc._width/2), mc._y, true)) {

                  mc._x -= speed;

           

              }

           

              if (border.hitTest(mc._x-(mc._width/2), mc._y, true)) {

           

                  mc._x += speed;

           

              }

           

              if (border.hitTest(mc._x, mc._y+(mc._height/2), true)) {

           

                  mc._y -= speed;

           

              }

           

              if (.border.hitTest(mc._x, mc._y-(mc._height/2), true)) {

           

                  mc._y += speed;

           

              }

           

          }

          • 2. Re: I need better code for moving an object
            Shad0wdrag0n Level 1

            Is there a better way to handle the hitTest, or is that actually OK? How do I make it so that the object keeps facing in the direction it was moving when you stop pressing the arrow key, rather than returning to frame 1?

            • 3. Re: I need better code for moving an object
              kglad Adobe Community Professional & MVP

              if those hittests work for you, leave them.

               

              if you don't want to return to mc's first frame when nothing is down why do you have:

               

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

                      mc.gotoAndStop(1);

                  }

               

              p.s.  please mark this thread as answered, if you can.

              • 4. Re: I need better code for moving an object
                Shad0wdrag0n Level 1

                That bit of code just stops the walk cycle, otherwise the player MC would keep playing. I'm not sure how to do it so the player MC goes to a different frame depending on which key was just pressed. Is there a code I can add that detects when a specific key is released? So, for example, when the left key is released it goes to "standLeft", and when the right key is released it goes to "standRight", etc.

                • 5. Re: I need better code for moving an object
                  kglad Adobe Community Professional & MVP

                  you can use a keylistener.  it has an onKeyUp method.