Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
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 343 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.

1  
2  
3  
4  
           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 Coder


Medals: 29



« 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
1  
 else if(statement) 
.

new code:

1  
2  
3  
4  
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
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2013-10-31 11:09:19 »

So say
1  
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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (29 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (31 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!