在我的游戏中,我需要一个类来扫描列表中所有节点之间的交叉点,然后调用抽象方法。例如,如果一个程序有100个球,则该类将检测任意两个球之间的任何碰撞。问题是,即使没有碰撞,该类也会调用该方法。然后,它根本没有检测到碰撞。到目前为止,这是我的代码:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.ImageView;
/**
*
* @author Jaca
*/
public abstract class massCollision {
private final ObservableList<ImageView> oli;
private final Thread t;
public massCollision() {
oli = FXCollections.observableArrayList();
t = new Thread(){
@Override
public void run(){
while(true){
for (ImageView a : oli) {
for(ImageView b : oli){
if(!a.equals(b)){ //makes sure thread doesn't check an object to itself
System.out.println("scanning");
if(a.intersects(b.getBoundsInParent())){ //checks for intersections
collision(a, b); //calls the abstract method collision
System.out.println("COLLISION!!!");
}
}else{
System.out.println("matched"); //lets user know when a duplicate is rejected
}
}
}
Thread.yield();
}
}
};
}
public void add(ImageView n){
oli.add(n);
}
public void remove(ImageView n){
oli.remove(n);
}
public void start(){
t.start();
}
@SuppressWarnings("deprecation")
public void stop(){
t.stop();
}
public abstract void collision(ImageView firstOffense, ImageView secondOffense);
}
有谁能告诉我我做错了什么?
答案 0 :(得分:0)
尝试以一种方式检查碰撞。 也许交叉方法无法正常工作。
在我的游戏中,我建造了一个这样的类:
public static void detectCollectableObjects(SimpleEntity entity, Game game) {
double x = entity.getLayoutX() - (entity.getWidth() / 2);
double y = entity.getLayoutY() - (entity.getHeight() / 2);
for (WeaponSpawn spawn : game.getWeaponSpawns()) {
if (spawn.isWeaponShowing()
&& (spawn.getLayoutX() <= x && spawn.getLayoutX()
+ spawn.getWidth() >= x)
&& (spawn.getLayoutY() <= y && spawn.getLayoutY()
+ spawn.getHeight() >= y)) {
entity.pickUpWeapon(spawn.getWeapon().clone());
spawn.weaponIsPickedUp();
break;
}
}
}
它不是最好的,但它对我有用