Skip to main content
Participant
January 22, 2013
Answered

Player moves diagonally and not straight. ???

  • January 22, 2013
  • 1 reply
  • 1122 views

Provided below is the code for my class.

It all works fine for controlling the player instance when I don't COMMENT the onKeysUp.

I wanted to create a constantly moving character so I removed the onKeysUp.  The first time you press a direction it moves straight, but then it moves only diagonally. 

I notice that this little guy could be causing me my newborn headache:

  function onEntersFrame(event:Event):void

  {

  //Move the player

  player.x += vx;

  player.y += vy;

}

Any ideas of changing this do stop the diagonal mumbo jumbo?

(((((((((((FULL CODE BELOW)))))))))))))

package

{

          import flash.display.MovieClip;

          import flash.events.KeyboardEvent;

          import flash.ui.Keyboard;

          import flash.events.Event;

          public class Main_Character_Two extends MovieClip

          {

                    var vx:int;

                    var vy:int;

                    public function Main_Character_Two()

                    {

                              init();

                    }

                    function init():void

                    {

                              //initialize variables

                              vx = 0;

                              vy = 0;

                              //Add event listeners

                              stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeysDown);

                              /*stage.addEventListener(KeyboardEvent.KEY_UP, onKeysUp);*/

                              addEventListener(Event.ENTER_FRAME, onEntersFrame);

                    }

                    function onKeysDown(event:KeyboardEvent):void

                    {

                              if (event.keyCode == Keyboard.LEFT)

                              {                                                                                                                        // v stands for velocity whichi sbetter than uising speed.

                                        vx = -5;                                                                                          // vx is the velocity of the direction x

                              }

                              else if (event.keyCode == Keyboard.RIGHT)

                              {

                                        vx = 5;

                              }

                              else if (event.keyCode == Keyboard.UP)

                              {

                                        vy = -5;

                              }

                              else if (event.keyCode == Keyboard.DOWN)

                              {

                                        vy = 5;

                              }

                    }

                    /*function onKeysUp(event:KeyboardEvent):void

                    {

                              if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)

                              {

                                        vx = 0;

                              }

                              else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)

                              {

                                        vy = 0;

                              }

                    }*/

                    function onEntersFrame(event:Event):void

                    {

                              //Move the player

                              player.x += vx;

                              player.y += vy;

                    }

          }

}

This topic has been closed for replies.
Correct answer Ned Murphy

If you only want to move straight at all times based on the last key pressed, try the following.  It is just a quick thought I had based on what you say you want...

Keep that small function and modify the onKeysDown function to add the one line shown in red:

                    function onKeysDown(event:KeyboardEvent):void

                    {

                             vx = vy = 0;

                              if (event.keyCode == Keyboard.LEFT)

                              {                                                                                                                         // v stands for velocity whichi sbetter than uising speed.

                                        vx = -5;                                                                                          // vx is the velocity of the direction x

                              }

                              else if (event.keyCode == Keyboard.RIGHT)

                              {

                                        vx = 5;

                              }

                              else if (event.keyCode == Keyboard.UP)

                              {

                                        vy = -5;

                              }

                              else if (event.keyCode == Keyboard.DOWN)

                              {

                                        vy = 5;

                              }

                    }

1 reply

Ned Murphy
Legend
January 22, 2013

Yes, that small function will have you constantly travelling in a diagonal since it changes both x and y at the same time.  Without your onKeyUp function you have nothing the stop the progress via making vx/vy = 0.

Participant
January 22, 2013

I tried deleting it and then nothing moves.  Any ideas how to make it so the player is constantly moving in the direction (STRAIGHT) so that I don't need to hold a key down?

Ned Murphy
Ned MurphyCorrect answer
Legend
January 22, 2013

If you only want to move straight at all times based on the last key pressed, try the following.  It is just a quick thought I had based on what you say you want...

Keep that small function and modify the onKeysDown function to add the one line shown in red:

                    function onKeysDown(event:KeyboardEvent):void

                    {

                             vx = vy = 0;

                              if (event.keyCode == Keyboard.LEFT)

                              {                                                                                                                         // v stands for velocity whichi sbetter than uising speed.

                                        vx = -5;                                                                                          // vx is the velocity of the direction x

                              }

                              else if (event.keyCode == Keyboard.RIGHT)

                              {

                                        vx = 5;

                              }

                              else if (event.keyCode == Keyboard.UP)

                              {

                                        vy = -5;

                              }

                              else if (event.keyCode == Keyboard.DOWN)

                              {

                                        vy = 5;

                              }

                    }