好吧,现在我正在研究一个基于布朗运动的小型锐化游戏,我正在研究撞击壁功能的粒子,但它不起作用。基本上,我用弧度表示粒子的方向,每当它碰到墙时,我都会将Pi弧度添加到方向来翻转它,但由于某种原因,它要么不会被调用,要么不会工作。这些是我拥有的代码,欢迎任何建议。我有一些(阅读:EXTREMELY)破的命中墙功能,我不明白我在做什么错
import java.awt.*;
public class Particle implements Actor {
protected double xPos;
protected double yPos;
protected Velocity v;
protected Color hue;
protected boolean needsUpdate;
public Particle(){
this((Math.random()*500),(Math.random()*500),new Velocity((int)(Math.random()*500),(Math.random()*Velocity.TAU)));
}
public Particle(double x, double y, Velocity vel){
xPos=x;
yPos=y;
v=vel;
hue=Color.red;
needsUpdate=false;
}
public void draw(Graphics g) {
g.setColor(hue);
g.fillOval((int)xPos, (int)yPos, 16, 16);
}
public void act() {
xPos+=v.getSlopefromDirection();
yPos+=1;
}
public void onHitWall(int dir) {
v.setDirection((v.getDirection()+Math.PI)%(Math.PI*2));
}
public void onHitOther(Actor other) {
}
public boolean canCollide() {
return true;
}
public int getLeftX() {
return (int)xPos;
}
public int getRightX() {
return (int)xPos+4;
}
public int getTopY() {
return (int)yPos;
}
public int getBottomY() {
return (int)yPos+4;
}
}
这是我使用,以显示它的类:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import static java.lang.System.out;
public class Feild extends JFrame{
protected ArrayList<Actor> actors;
private final int size=500;
protected Thread th;
public Feild(ArrayList<Actor> a){
super("A Brownian Love Story");
setSize(size, size);
actors=a;
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
th = new Thread();
while(true){
paint(getGraphics());
}
}
public void paint(Graphics g){
super.paint(g);
//g.fillRect(0, 0, 500, 500);
for(int i=0;i<actors.size();i++){
if(actors.get(i).getLeftX()<=0)
actors.get(i).onHitWall(1);
if(actors.get(i).getRightX()>=500)
actors.get(i).onHitWall(2);
if(actors.get(i).getTopY()<=0)
actors.get(i).onHitWall(1);
if(actors.get(i).getBottomY()>=500)
actors.get(i).onHitWall(2);
actors.get(i).act();
actors.get(i).draw(g);
}
for(int i=0;i<1000;i++){out.println(i);}
}
}
所以我试图改变涂料的功能作用后碰撞检测,它仍然看起来像onHitWall即将被跳过,尽管在放置打印语句后它不是。
首先,它是一个“字段”,而不是“Feild”。现在,什么是'速度'?此外,在寻求帮助之前,您至少应尝试调试代码 - 在“onHitWall”中插入打印语句很简单。最后,“这不起作用”对问题没有帮助。它有什么作用? – Borealid 2012-02-19 03:04:03
1.)手指的滑动我没有打算纠正。 2.)从描述可以推断,它实质上是一个数据结构,其中包含一个速度和方向的组成部分 3)描述的非触发部分暗示它看起来像跳过 – natman3400 2012-02-19 03:10:39