Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (710)
Games in Android Showcase (212)
games submitted by our members
Games in WIP (784)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  movement key press  (Read 882 times)
0 Members and 1 Guest are viewing this topic.
Offline James van Dalen

Senior Newbie

« Posted 2013-10-31 09:53:41 »

Hi all,

My question is how do I make a character move in only the direction of the last arrow key input.

So if you press up the character moves upward and then press left without letting go of the up arrow, the character stops moving up and then moves left instead. I have tried and I presume the solution is incredibly simple but it just doesn't want to reveal itself to me.

Here is the code I currently tried, it works but only in one of the axis, when moving up or down the character will change direction if you press either the left or right keys. However if you start with the left or right keys, then the character doesn't change when you press the up or down.

           if(input.left.isPressed()) mx--;
      else if(input.right.isPressed()) mx++;
      else if(input.up.isPressed()) my--;
      else if(input.down.isPressed()) my++;

I have also tried to make it that when you press one of the arrow keys all the other keys are released and that had the same result as this code, one axis it works and the other it does not.

Sorry if it is difficult to understand the question, my brain is fried from trying to figure this out, the quality of the question could be ruined because of that.
Offline quew8

JGO Knight

Medals: 53

« Reply #1 - Posted 2013-10-31 10:05:56 »

Use mx and my. Whenever any key is pressed (as in when it goes down not when it is down) set both mx and my to zero and then set them as appropriate for the key that is pressed.
Offline Phased
« Reply #2 - Posted 2013-10-31 10:11:15 »

remove else from all the statements

currently in your current set of if statements, if one key is pressed, the other cant be pressed.

in your current code, left has priority, if you press left and down, left will always execute as its the first if statement. to fix this, you must have a separate if statement for each key press, so do not use
 else if(statement) 

new code:

if(input.left.isPressed()) mx--;
if(input.right.isPressed()) mx++;
if(input.up.isPressed()) my--;
if(input.down.isPressed()) my++;

remember, when you have if else statements, it will continue to check each else if until the statement is TRUE, it will not check all if it does not have to.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline James van Dalen

Senior Newbie

« Reply #3 - Posted 2013-10-31 11:01:58 »

this is how I had it originally, but this allows players to move in diagonal lines... and that is now what I want... I need it to move in only one direction at a time
Online Opiop
« Reply #4 - Posted 2013-10-31 11:09:19 »

So say
if(!input.left.isPressed() && input.up.isPressed()) my--;

Do this for all the angles. Its simple.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

numerical (86 views)
2017-02-21 07:32:16

numerical (87 views)
2017-02-21 07:31:46

theagentd (191 views)
2017-02-18 13:42:33

theagentd (195 views)
2017-02-18 13:35:16

h.pernpeintner (1357 views)
2017-01-24 22:39:11

h.pernpeintner (1345 views)
2017-01-24 22:38:32

Galdo (1907 views)
2017-01-12 13:44:09

Archive (1989 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2525 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2312 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!