Hi, how is it possible to handle multiple keys at same time. I am making platformer game. Now if I push forward it goes forward until I release button or hit some else button example jump. What should I do to achieve that it wouldnt stop going forward but jumps forward (Forward button still pressed).
package net.viped.platformer;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Platform extends Canvas implements Runnable {
public static final int WIDTH = 800;
public static final int HEIGHT = 622;
Thread thread;
double playerx;
double playery;
Rectangle2D player;
boolean jumpKey;
boolean jumping;
boolean left;
boolean right;
long time, time2;
boolean done = true;
public Platform() {
setFocusable(true);
addKeyListener(new ListenKeys());
initNewGame();
}
public void initNewGame() {
jumping = false;
right = false;
left = false;
playerx = 100;
playery = 370;
player = new Rectangle2D.Double(playerx, playery, 30, 30);
}
public void Render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
player.setRect(playerx, playery, 30, 30);
g2.fill(player);
g.dispose();
g2.dispose();
bs.show();
}
boolean h = false;
double playeryMem;
public void updateGame() {
// JUMPING
if (jumpKey && done == true) {
playeryMem = playery;
done = false;
time = System.currentTimeMillis();
if (!jumping) {
jumping = true;
}
}
if (jumping) {
if (System.currentTimeMillis() < time + 1000) {
playery -= 2;
} else if (playery < playeryMem) {
playery += 2;
if (playery == 370) {
done = true;
}
}
}
// GO RIGHT
if (right) {
System.out.println(true);
playerx++;
}
// GO LEFT
if (left) {
playerx--;
}
}
public void start() {
initNewGame();
thread = new Thread(this);
thread.start();
}
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateGame();
Render();
}
}
public class ListenKeys extends KeyAdapter {
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
jumpKey = true;
break;
case KeyEvent.VK_RIGHT:
right = true;
System.out.println("right");
break;
case KeyEvent.VK_LEFT:
left = true;
break;
}
}
public void keyReleased(KeyEvent e) {
jumpKey = false;
right = false;
left = false;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
Platform pl = new Platform();
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.add(pl);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
pl.start();
}
}
I tried debug with printing "right" when e.getkeycode() == e.vk_right and it stopped giving the right text to me after hittin up button.