Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 2527 times)
0 Members and 1 Guest are viewing this topic.
Offline hamidious

Innocent Bystander





« Posted 2005-10-14 07: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: 1


pixels! :x


« Reply #1 - Posted 2005-10-14 08: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
x+=dx
y+=dy

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 Member




Beyond mind, there is an awareness...


« Reply #2 - Posted 2005-10-14 10: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 Member





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

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

private boolean[] keys=new boolean[256];
public void KeyPressed(KeyEvent e)
{
       keys[e.getKeyCode()]=true;
}
public void keyReleased(KeyEvent e)
{
       keys[e.getKeyCode()]=false;
}

// in your move method...
if(keys[KeyEvent.VK_UP])//Up is pressed
{
      charecter.moveUp();
}
if(keys[KeyEvent.VK_RIGHT])//Up is pressed
{
      charecter.moveRIGHT();
}

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,
noam
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


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

keys[e.getKeyCode()&0xFF]=true;
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2005-10-19 23: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!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #6 - Posted 2005-10-29 03: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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
LinkedList buttonsPressed = new LinkedList();

public void keyPressed(KeyEvent e)
{
    buttonsPressed.add(e);
}

public void keyReleased(KeyEvent e)
{
    if (buttonsPressed.contains(e))
        buttonsPressed.remove(e);
}

private void processKeys()
{
    for (Iterator i = buttonsPressed.iterator(); i.hasNext();)
    {
        int code = ((KeyEvent)i.next()).getKeyCode();
        if (code == KeyEvent.VK_UP)
            moveUp();
        else if (code == KeyEvent.VK_DOWN)
            moveDown();
        //etc. etc.
    }
}


And voila. Does that make sense to you?

See my work:
OTC Software
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.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!