我不明白为什么在我超过球的前半部分后,当我控制的球击中移动球的右半部分时,它不会激活碰撞代码来杀死程序。
我将它设置为测量你控制的球与经过的球之间的距离。我仍然感到困惑,为什么我可以在没有程序自行结束的情况下降落在球的后半段。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
public class JumpingSprite extends JPanel implements KeyListener {
//public int yPos;
public static void main(String[] args) {
new JumpingSprite();
}
public JumpingSprite() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Mr.Baird");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
TestPane testPane = new TestPane();
frame.add(testPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
int getyPos(){
return yPos;
}
protected static final int SPRITE_HEIGHT = 60;
protected static final int SPRITE_WIDTH = 60;
private float vDelta;
private float rbDelta;
private float rbDegDelta;
public int yPos;
private float gDelta; // Gravity
private Timer engine;
private boolean bounce = false;
int ballx = 1000;
int bally = 500;
int ballw = 100;
int ballh = 100;
int ball2x = 1500;
int ball2y = 480;
int ball2w = 120;
int ball2h = 120;
int ball3x = 2500;
int ball3y = 0;
int ball3w = 600;
int ball3h = 600;
public TestPane() {
yPos = getPreferredSize().height - SPRITE_HEIGHT;
vDelta = 0;
gDelta = 3f;
rbDegDelta = 30f; //affects how many bounces mang
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "jump");
am.put("jump", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (yPos + SPRITE_HEIGHT == getHeight()) {
vDelta = -40 ;
rbDelta = vDelta;
bounce = true;
}
}
});
engine = new Timer(15, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int height = getHeight();
if (height > 0) {
if (bounce) {
yPos += vDelta;
vDelta += gDelta;
// If the sprite is not on the ground
if (yPos + SPRITE_HEIGHT >= height) {
// put on le ground
yPos = height - SPRITE_HEIGHT;
if (rbDelta >= 0) {
// Stop bouncin
bounce = false;
} else {
// Add the re-bound degregation delta to the re-bound delta
rbDelta += rbDegDelta;
// Set the vDelta...
vDelta = rbDelta;
}
}
}
}
if (Math.sqrt((Math.pow((500 - (ballx)), 2)) + Math.pow((yPos - bally), 2)) <= (50)) {
System.exit(0);
}
if (Math.sqrt((Math.pow((500 - (ball2x)), 2)) + Math.pow((yPos - ball2y), 2)) <= (60)) {
System.exit(0);
}
if (Math.sqrt((Math.pow((500 - (ball3x)), 2)) + Math.pow((yPos - ball3y), 2)) <= (350)) {
System.exit(0);
}
ballx = ballx-8;
ball2x = ball2x-8;
ball3x = ball3x-8;
repaint();
}
});
engine.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(1000, 600);
}
@Override
protected void paintComponent(Graphics g) {
//addin all them pretty colors to le object
super.paintComponent(g);
//Graphics2D g2d = (Graphics2D) g.create();
g.setColor(Color.black);
g.fillRect(0,0,1000,1000);
int width = getWidth() - 1;
int xPos = (width - SPRITE_WIDTH) / 2;
g.setColor(Color.blue);
g.fillOval(xPos, yPos, SPRITE_WIDTH, SPRITE_HEIGHT);
//g.dispose();
g.setColor(Color.red);
g.fillOval(ballx, bally, ballw, ballh);
g.setColor(Color.green);
g.fillOval(ball2x, ball2y, ball2w, ball2h);
g.setColor(Color.pink);
g.fillOval(ball3x, ball3y, ball3w, ball3h);
}
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
该问题的一般解决方案是计算两个球/圆的中心之间的(欧几里德)距离,并确定该长度是否小于它们的组合半径。另一种方法是从java.awt.Area
创建Shape
,然后使用Area.intersect(Area)!= null;