[Solved] How would you know if the key was being held? For LD23!

In the Ludum Dare right now, I am working on a game. I call this code to move the player’s position.

package src.longarmx.ld23;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Input implements KeyListener{
	
	public static int playerUpKey = KeyEvent.VK_W;
	public static int playerDownKey = KeyEvent.VK_S;
	public static int playerRightKey = KeyEvent.VK_D;
	public static int playerLeftKey = KeyEvent.VK_A;
	
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == playerUpKey){
			if(Player.playerY >= 0)
			Player.playerY -= Player.playerSpeed;
		}
		if(e.getKeyCode() == playerDownKey){
			if(Player.playerY <= World.worldHeight * World.blockSize - World.blockSize*2)
			Player.playerY += Player.playerSpeed;
		}
		if(e.getKeyCode() == playerRightKey){
			if(Player.playerX <= World.worldWidth * World.blockSize - World.blockSize)
			Player.playerX += Player.playerSpeed;
		}
		if(e.getKeyCode() == playerLeftKey){
			if(Player.playerX >= World.blockSize)
			Player.playerX -= Player.playerSpeed;
		}
	}

	public void keyReleased(KeyEvent e) {	}


	public void keyTyped(KeyEvent e) {	}
}

The bad thing about this is that when you hold down the key, the player’s position doesn’t keep updating. You have to keep pressing the key to get the player to move. I want to know how to keep the player moving while only having to hold down the key.

Thank you,
Longarmx

Use booleans. When dX boolean is true then give value to dX speed, otherwise dX speed is zero. Do it for Y too.

I would also rely on a update() method, instead of using the events to handle logic

Little example:

public class Input implements KeyListener {
	public Game game;

	public boolean keys[] = new boolean[65535];
	public boolean w, s, a, d;
		
	public Input(Game game) {
		this.game = game;
		game.addKeyListener(this);
	}
	
	public void update() {	
		w = keys[KeyEvent.VK_W];
		s = keys[KeyEvent.VK_S];
		a = keys[KeyEvent.VK_A];
		d = keys[KeyEvent.VK_D];
	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if (key > 0 || key < keys.length) {
			keys[key] = true;
		}
	}

	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		if (key > 0 || key < keys.length) {
			keys[key] = false;
		}
	}

	public void keyTyped(KeyEvent e) {
	}

}

And then call the update() from your main loop
Good luck!

Thank you do much! I am so tired right now I don’t know why I couldn’t figure this out by myself. :slight_smile: It works great now and hopefully I can get this game done in time. ;D

Thank You,
Longarmx

I just starting to create my entry now. Let’s see which one better :slight_smile:

I usually watch people code when LD is on

but there are only 240p streams, i couldnt even see what program the last one was using :frowning:

[quote]I just starting to create my entry now. Let’s see which one better :slight_smile:
[/quote]
Haha, I started creating mine 3 hours ago. You have no chance! :yawn: (I meant I have no chance ;D)

PS: I’m going to burn my graphics!

3 hours?! then I guess you already done a RPG game with complete self drawn sprite and all battle system :stuck_out_tongue:

No, I’m still a noob :wink: I’ve only gotten the world, player, coins, and customizeable maps semi-decent. I know, I’m going to be typing 100 wpm the hour before the competition is finished. ;D

Noob? there’s no way even a pro can do what I said. Oh wait, they use RPG maker. Ofc they can. :wink:

Mine is finished. The title is Narrow

What? No! I made that and more in an hour. I had only been learning java for 4 months.