gpt4 book ai didi

java - Hitbox 不起作用

转载 作者:行者123 更新时间:2023-12-01 22:50:58 26 4
gpt4 key购买 nike

在此代码中,碰撞盒没有感知到任何东西。这当圆形和矩形接触时,检查两个碰撞框是否相交的 if 语句不会执行其中的操作。我该如何让它发挥作用?

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;

import javax.swing.JFrame;

public class RunPaintGUI extends JFrame implements KeyListener {
int run = 10;
int x = 30;
int y = 30;
int score = 0;
Random randomgenerator = new Random();
int a = randomgenerator.nextInt(1220);
int b = randomgenerator.nextInt(700);

public static void main(String[] args) {

RunPaintGUI RunPaintGUI = new RunPaintGUI();
}

public RunPaintGUI() {
this.setSize(1275, 775);
this.setResizable(false);
this.setTitle("game");
this.setVisible(true);
this.addKeyListener(this);
}

public void paint(Graphics g) {
super.paint(g);

g.fill3DRect(x, y, 60, 60, true);

g.fillOval(a, b, 50, 50);

g.drawString("score: " + score, 600, 50);

}

Rectangle2D hitbox = new Rectangle(x, y, 60, 60);
Ellipse2D.Double hitbox1 = new Ellipse2D.Double(a, b, 50, 50);

public void checkiftouching() {
if (hitbox1.intersects(hitbox))
;

System.out.println("you won");
a = randomgenerator.nextInt(1220);
b = randomgenerator.nextInt(720);
repaint();
score = score + 1;
}

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x = x - run;
hitbox.setFrame(x, getY(), getWidth(), getHeight());
hitbox1.setFrame(a, b, 50, 50);
repaint();

// System.out.println(x);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
x = x + run;
hitbox.setFrame(x, getY(), getWidth(), getHeight());
hitbox1.setFrame(a, b, 50, 50);
repaint();

// System.out.println(x);
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
y = y - run;
hitbox.setFrame(getX(), y, getWidth(), getHeight());
hitbox1.setFrame(a, b, 50, 50);
repaint();

// System.out.println(y);
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
y = y + run;
hitbox.setFrame(getX(), y, getWidth(), getHeight());
hitbox1.setFrame(a, b, 50, 50);
repaint();

// System.out.println(y);
}
}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
}

最佳答案

你会注意到

public void checkiftouching(){

从未被调用过。此外,这个 if 语句末尾有一个分号,所以它的作用很少。

if (hitbox1.intersects(hitbox));

始终在 if、while、for、开关后面加上完整的括号 { .. }

如果您使用 IDE,它应该会为您突出显示这些错误。

关于java - Hitbox 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com