Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
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  
  Diagonal keyboard movement in java  (Read 7332 times)
0 Members and 1 Guest are viewing this topic.
Offline hamidious

Innocent Bystander

« Posted 2005-10-14 05:47:09 »

Ok I have a square. I want to use the 4 directional keyboard buttons to move it around by changing its x and y cooardinates. I can move it anyplace except
for diagonally. This is because only the last pressed and held down key is firing the event and keyTyped( ). Therefore, to move diagonally, you have to
do something like this: UP, RIGHT, UP,RIGHT...etc rather than keeping the buttons pressed down simultaneously which will result in a non-diagonal movement
according to the last pressed button. If anyone can help me by first explaining
the concept of what I should do, then if necessary show me some code I would appreciate it.

Another thing is, when I want to move into a certain direction, let's say to the right, I keep pressing right. However, the first movement is a movement
followed by a small delay, then the movement continues normally. If you want to know what I mean just keep holding down a letter button now and see how the
first typed letter and the rest of the letters are not typed in the same speed. How do I get rid of this to make the movement more appropriate? Concept is always
more important so please don't just paste some code and tell me to use it, thank you.

Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #1 - Posted 2005-10-14 06:06:39 »

Use flags for the key states. Eg if a button is pressed you set the variable for that button to true and if its released you set it to false.

In your loop you simply check the state of the variable and react accordingly. Like

//get the deltas
if right dx+=5
if left dx-=5
if down dx+=5
if up dx-=5
//update player position

Note that I didn't use else... this way pressing left and right together results in no horizontal movement (+5-5=0).

Edit: Oh and using deltas instead of modfying the position directly allows things like friction and acceleration to be added easily.

弾幕 ☆ @mahonnaiseblog
Offline hvor2

Junior Devvie

Beyond mind, there is an awareness...

« Reply #2 - Posted 2005-10-14 08:12:15 »

Yes, DONT USE ELSE!  I am using something like

        if (Keyboard.isKeyDown(Keyboard.KEY_W) )) {
           camera.move(camera.getMoveParam() * time * 10);

        if (Keyboard.isKeyDown(Keyboard.KEY_A) )) {
            camera.strafe(-camera.getMoveParam() * time / 2);

and it is working fine. When program is running, and you have pressed W + A,  you first move camera forward and then to the left...

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

Junior Devvie

« Reply #3 - Posted 2005-10-14 08:14:49 »

further more, you set your flags with the following methods:

private boolean[] keys=new boolean[256];
public void KeyPressed(KeyEvent e)
public void keyReleased(KeyEvent e)

// in your move method...
if(keys[KeyEvent.VK_UP])//Up is pressed
if(keys[KeyEvent.VK_RIGHT])//Up is pressed

also, it'll be good practice to implement a focus listener and set all the keys to false if you lose focus, as opposed to those games where if you minimize the window and then return to it, a key can get "stuck" and your charecter keeps on shooting though you're not pressing any key.
hope this helps,
Offline Anon666

Junior Devvie

aka Abuse/AbU5e/TehJumpingJawa

« Reply #4 - Posted 2005-10-14 13:02:35 »

Offline Jeff

JGO Coder

Got any cats?

« Reply #5 - Posted 2005-10-19 21:44:35 »

Just a side note. You can avoid all these AWT type behaviors by using JInput.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Offline Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

« Reply #6 - Posted 2005-10-29 01:37:06 »

Umm, you are getting some pretty random helping here. Some people are giving you LWJGL help, for example. And I think using flags, while certainly a way to do it, is unnecessary and non-intuitive.

With regular AWT you can only keep track of one key at a time. However, there is a simple solution: make a LinkedList and add keys as they are pressed, remove them as they are released. KeyTyped is not needed. You can see this is the same general concept as the flag solution, but much less memory is allocated, you can handle every possible key press, and it is completely intuitive. This is how I implement all my AWT games.

LinkedList buttonsPressed = new LinkedList();

public void keyPressed(KeyEvent e)

public void keyReleased(KeyEvent e)
    if (buttonsPressed.contains(e))

private void processKeys()
    for (Iterator i = buttonsPressed.iterator(); i.hasNext();)
        int code = ((KeyEvent);
        if (code == KeyEvent.VK_UP)
        else if (code == KeyEvent.VK_DOWN)
        //etc. etc.

And voila. Does that make sense to you?

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  

hadezbladez (739 views)
2018-11-16 13:46:03

hadezbladez (379 views)
2018-11-16 13:41:33

hadezbladez (729 views)
2018-11-16 13:35:35

hadezbladez (191 views)
2018-11-16 13:32:03

EgonOlsen (2389 views)
2018-06-10 19:43:48

EgonOlsen (2550 views)
2018-06-10 19:43:44

EgonOlsen (1477 views)
2018-06-10 19:43:20

DesertCoockie (2142 views)
2018-05-13 18:23:11

nelsongames (1945 views)
2018-04-24 18:15:36

nelsongames (2624 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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!