This code is supposed to stop an object from passing through it, ans if an object hits it, make it so it has to wait 1 second before it can be interacted with again. I thought the best place to but the timer would be after the keylistener, but when I tried to put an if statement down, it just came up as an error, even though it seems to be complete.
What can I do to fix this, and is there a better way to do this?
I put the problem statement towards the middle, and added comment lines at the start and end of the if statement. Thank you for your help.
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class Imject {
int x = 30;
int y = 30;
int xa = 0;
int ya = 0;
private CrcGame Game;
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W)
if (collision()){
ya = 0;
y =- y;
}
else {ya = 1;
}
if (e.getKeyCode() == KeyEvent.VK_S)
if (collision()){
ya = 0;
}
else {ya = -1;
}
if (e.getKeyCode() == KeyEvent.VK_A)
if (collision()){
xa = 0;
}
else {xa = 1;
}
if (e.getKeyCode() == KeyEvent.VK_D)
if (collision()){
xa = 0;
}
else {xa = -1;
}
}
public void keyReleased(KeyEvent e) {
ya = 0;
xa = 0;
}
//Starts
if (collision()){
}
//Ends
void move() {
if (x + xa > 0)
x = x + xa;
if (y + ya > 0)
y = y + ya;
}
public Imject(CrcGame Game) {
this.Game= Game;
}
private boolean collision() {
return Game.player.getBounds().intersects(getBounds());
}
public void paint(Graphics2D g) {
g.fillRect(x, y, 100, 20);
}
public Rectangle getBounds() {
return new Rectangle(x, y, 100, 20);
}
}
Aucun commentaire:
Enregistrer un commentaire