Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Player moves diagonally and not straight. ???

New Here ,
Jan 21, 2013 Jan 21, 2013

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;

                    }

          }

}

TOPICS
ActionScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 21, 2013 Jan 21, 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)

                              {                  

...
Translate
LEGEND ,
Jan 21, 2013 Jan 21, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 21, 2013 Jan 21, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 21, 2013 Jan 21, 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;

                              }

                    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 21, 2013 Jan 21, 2013

That wouldn't let me do it... but it got me thinking... and figured it out  I added code similar to what is underlined in each else if .  That did the trick.  No more Diagonal shuffle.  Thank you for your help Ned.

                              if (event.keyCode == Keyboard.LEFT)

                              {                                                                                                             

                                        vx = -5;   

                                        vy = 0;                                                                            

                              }

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

                              {

                                        vx = 5;

                                        vy = 0;    

                                       

                              }

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

                              {

                                        vy = -5;

                                        vx = 0;

                              }

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

                              {

                                        vy = 5;

                                        vx = 0;

                              }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 21, 2013 Jan 21, 2013

It should have worked.  I just tried it and it works fine as I showed.  At least the problem is solved for now.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 21, 2013 Jan 21, 2013

hmmm  That's odd.  I think I did something else that messed it up.  I will try that again later.  Thanks again.  Time to call it a night.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 22, 2013 Jan 22, 2013

AHA!  It did work.  Thank you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 22, 2013 Jan 22, 2013
LATEST

You're welcome.  I just had a realized that the way you had it might be better because any keypress other than one of the arrow keys will stop the motion - I don't know if that is something you want or not. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines