I'm making a 2D game in java.
In the game, pressing the left and right arrow keys rotates a sprite and manipulates a variable named 'angle'. Pressing the up arrow key, computes the direction where the sprite should move depending on the 'angle' variable.
This works, but I'm unable to manipulate the angle while moving, aka have left or right keys be pressed at the same time the up arrow is pressed.
Pressing the up arrow makes the program ignore the fact the one of the arrow keys is pressed.
I have searched the forum and found one suggestion how to handle that, but I'm not sure it's the best for my program. (What I found: How do I handle simultaneous key presses in Java?)
Here's some code:
Relevant methods of Thing
class:
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key==KeyEvent.VK_LEFT){ setAngle(getAngle()-5);
leftPressed=true; // *new* }
if(key==KeyEvent.VK_RIGHT)setAngle(getAngle()+5);{
rightPressed=true; // *new* }
if(key==KeyEvent.VK_UP){
dy = (float) ( Math.sin( getAngle() ) ) * 5;
dx = (float) ( Math.sin( 90-getAngle() ) ) * 5;
if(leftPressed==true)angle-=5; // *new*
if(rightPressed==true)angle+=5; // *new*
}
public void move(){
x += dx;
y += dy;
}
}
The Board
class (the class that manages the game:)
package learningMovement;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener, KeyListener {
Thing t;
Timer timer;
public Board(){
setBackground(Color.BLACK);
timer = new Timer(10,this);
t = new Thing();
setFocusable(true);
timer.start();
addKeyListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.rotate( Math.toRadians(t.getAngle()), t.getX() + (t.getWidth()/2), t.getY() + (t.getHeight()/2) );
g2d.drawImage(t.getImage(),(int)t.getX(),(int)t.getY(),this);
}
public void actionPerformed(ActionEvent e){
t.move();
repaint();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key==KeyEvent.VK_LEFT)t.keyPressed(e);
if(key==KeyEvent.VK_RIGHT)t.keyPressed(e);
if(key==KeyEvent.VK_UP)t.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key==KeyEvent.VK_LEFT)t.keyReleased(e);
if(key==KeyEvent.VK_RIGHT)t.keyReleased(e);
if(key==KeyEvent.VK_UP)t.keyReleased(e);
}
public void keyTyped(KeyEvent arg0) {}
}
How do I fix this? Thanks