このコードは、オブジェクトがそれを通過するのを止めると想定されています。オブジェクトがそれにヒットした場合は、1秒待ってから再度やりとりする必要があります。私はタイマーがkeylistenerの後になると思っていましたが、if文を落とそうとしたときに、完了したように見えますが、エラーとして表示されました。if文がエラーになるのはなぜですか?
これを修正するにはどうすればよいですか?これを行うにはより良い方法がありますか?
私は問題文を真ん中に置き、if文の先頭と最後にコメント行を追加しました。ご協力ありがとうございました。
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);
}
}
いつ実行されると思いますか? – njzk2
あなたは 'if'文がクラスの真ん中に座ることができないことを認識していますか? – Li357
すべてのifステートメントの中括弧はどこにありますか? –