JavaFX游戏中的图形:帮助我解决这个问题
我目前正在制作一个简单的2d游戏,比如flappy bird。对于那些不熟悉这一点的人来说,这款游戏是一款侧滚游戏,玩家可以控制一个物体,试图在一排排霓虹管之间飞行,而不会碰到它们。在这里你可以看到不明飞行物(initFrog)并没有完全接触到霓虹管。但是,由于霓虹管上的辉光效果是.png文件的一部分,UFO将其识别为交点。有一个计时器,设置为在两个相交时停止(查看下面的最后一段代码) 那么,当不明飞行物撞上霓虹灯,而不仅仅是霓虹灯发光时,我怎么能让计时器停止呢JavaFX游戏中的图形:帮助我解决这个问题,java,javafx,graphics,graphics2d,Java,Javafx,Graphics,Graphics2d,我目前正在制作一个简单的2d游戏,比如flappy bird。对于那些不熟悉这一点的人来说,这款游戏是一款侧滚游戏,玩家可以控制一个物体,试图在一排排霓虹管之间飞行,而不会碰到它们。在这里你可以看到不明飞行物(initFrog)并没有完全接触到霓虹管。但是,由于霓虹管上的辉光效果是.png文件的一部分,UFO将其识别为交点。有一个计时器,设置为在两个相交时停止(查看下面的最后一段代码) 那么,当不明飞行物撞上霓虹灯,而不仅仅是霓虹灯发光时,我怎么能让计时器停止呢 private Node
private Node initFrog() {
ImageView falc = new ImageView();
falc.setImage(milFalc);
falc.setTranslateY(300-39);
falc.setTranslateX(240);
falc.setScaleX(.3);
falc.setScaleY(.3);
return falc;
}
private Node ships() {
int haut = (int)(Math.random()*600);
ImageView sheep = new ImageView();
sheep.setImage(Orbs);
sheep.setTranslateY(haut-500);
sheep.setTranslateX(800);
sheep.setScaleX(.017);
sheep.setScaleY(.017);
root.getChildren().add(sheep);
return sheep;
}
private Node initNeon() {
int haut = (int)(Math.random()*260);
ImageView neona = new ImageView();
neona.setImage(neon1);
neona.setTranslateY(haut-200);
neona.setTranslateX(800);
neona.setRotate(90);
neona.setScaleX(1.);
neona.setScaleY(.8);
root.getChildren().add(neona);
return neona;
}
private Node SpawnzCar() {
int hauts = (int)(Math.random() *280);
ImageView neona = new ImageView();
neona.setImage(neon2);
neona.setTranslateY(hauts+520);
neona.setTranslateX(800);
neona.setRotate(90);
neona.setScaleX(1.);
neona.setScaleY(.8);
root.getChildren().add(neona);
return neona;
} private void onUpdate() {
for (Node car : cars)
car.setTranslateX(car.getTranslateX() - 11);
if (Math.random() <= 0.07 ) {
cars.add(SpawnzCar());
// cars.add(ships());
cars.add(initNeon());}
checkState();
}
private void checkState() {
for (Node car : cars) {
if (car.getBoundsInParent().intersects(frog.getBoundsInParent())) {
frog.setTranslateX(frog.getTranslateX());
timer.stop();
frog.setTranslateY(frog.getTranslateY());
return;
} }
private Node initFrog(){
ImageView falc=新ImageView();
falc.setImage(milFalc);
猎鹰队(300-39);
隼.setTranslateX(240);
falc.setScaleX(.3);
falc.setScaleY(.3);
返回鹰;
}
专用节点ships(){
int haut=(int)(Math.random()*600);
ImageView=新的ImageView();
绵羊。setImage(球体);
绵羊。setTranslateY(haut-500);
绵羊:毛平菇(800);
绵羊.setScaleX(.017);
绵羊。塞斯卡利(.017);
root.getChildren().add(sheep);
还羊;
}
私有节点initNeon(){
int haut=(int)(Math.random()*260);
ImageView neona=新的ImageView();
neona.setImage(neon1);
neona.setTranslateY(haut-200);
褐飞虱(800);
坐骨旋转(90);
neona.setScaleX(1);
neona.setScaleY(.8);
root.getChildren().add(neona);
返回neona;
}
专用节点SpawnzCar(){
int hauts=(int)(Math.random()*280);
ImageView neona=新的ImageView();
neona.setImage(neon2);
neona.setTranslateY(上+520);
褐飞虱(800);
坐骨旋转(90);
neona.setScaleX(1);
neona.setScaleY(.8);
root.getChildren().add(neona);
返回neona;
}私有void onUpdate(){
用于(节点车辆:车辆)
car.setTranslateX(car.getTranslateX()-11);
如果(Math.random(),而不是测试png文件是否相互相交,您可以使用坐标手动检查修改的命中框。我看不出您在哪里存储ufo和管道的坐标值,因此我将使用伪代码作为示例
public boolean isCollision(){
if(ufo.getX() + ufo.width() > pipe.getX() + pipe.getOffSetWidth() && ufo.getX() < pipe.getX() + pipe.width() - pipe.getOffSetWidth()){
if(\\check for y value of height, this can be different depending on your way of storing pipes){
return true;
}
}
}
public boolean isCollision(){
如果(ufo.getX()+ufo.width()>pipe.getX()+pipe.getOffSetWidth()&&ufo.getX()
您希望offSetWidth()值是管道图像边缘与希望发生碰撞的管道部分之间的长度(以像素为单位)。谢谢!!非常有用