我有两个游戏对象,即矩形,玩家和敌人。我希望玩家在Y轴上跳跃,敌人沿X轴滑动。当玩家的X,Y位置与敌人匹配时,它应该杀死玩家。到目前为止,我无法通过AnimationTimer()来检查每个帧的常量X位置和Y位置。它仅检查应用程序何时首次启动并保留这些值。
如何检查每个帧的两个矩形的X位置和Y位置?
AnimationTimer animator = new AnimationTimer() {
public void handle(long arg0) {
//update
double playX = player.getX(), enemX = enemy.getX();
double playY = player.getY(), enemY = enemy.getY();
System.out.println("Player's X: "+ playX + "\n" + "Enemy's X: " + enemX);
System.out.println("Player's Y: "+ playY);
if (playX == enemX && playY == enemY){
System.out.println("Player has Collided with Enemy");
}
}//handle
};
animator.start();//animation
答案 0 :(得分:0)
尝试类似:
Shape intersect = Shape.intersect(player, enemy);
if(intersect.getBoundsInLocal().getWidth() != -1)
{
System.out.println("Player has Collided with Enemy");
}