【发布时间】:2023-11-18 03:32:02
【问题描述】:
我对 JavaFX 灯光效果有疑问。我有一个游戏需要在同一个窗格上使用多个点灯,但我还没有设法做到这一点,如果它甚至可能的话。现在我有一个窗格和上面的所有元素。
这似乎是一个不好的方法,所以如果有人知道为 2D 游戏添加光源的更好方法,我将非常感谢您的帮助!
似乎只能将一个灯光效果附加到窗格,因为每当我尝试设置新的灯光效果时,另一个都会被删除。对于这个项目来说,一盏灯是不够的。如果有更好的添加灯光的方法,请告诉我! 也许将灯连接到块上,然后以某种方式使其在窗格上发光? 代码如下:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class game extends Application{
@Override
public void start(Stage alku) throws Exception {
Pane test=new Pane();
Rectangle box = new Rectangle(200,200);
box.setFill(Color.WHITE);
box.setTranslateX(50);
box.setTranslateY(50);
test.getChildren().add(box);
Rectangle box2 = new Rectangle(200,200);
box2.setFill(Color.WHITE);
box2.setTranslateX(50);
box2.setTranslateY(300);
test.getChildren().add(box2);
Scene scene = new Scene(test,400,400);
Lighting light = new Lighting();
Light.Point l = new Light.Point();
l.xProperty().set(70);
l.yProperty().set(200);
l.setZ(50);
l.setColor(Color.GREEN);
light.setLight(l);
test.setEffect(light);
Lighting light2 = new Lighting();
Light.Point l2 = new Light.Point();
l2.xProperty().set(20);
l2.yProperty().set(200);
l2.setZ(50);
l2.setColor(Color.RED);
light2.setLight(l2);
test.setEffect(light2);
alku.setTitle("light test");
alku.setScene(scene);
alku.show();
}
public static void main(String[] args) {
launch(args);
}
}
this is how it looks at the moment
所以“光”被覆盖了。
【问题讨论】:
-
我认为你必须创建两个
Lighting实例并将它们组合在一个Blend中。不过,我对Effects 没有太多经验。如果您提供minimal reproducible example,有人可能会为您测试。 -
@James_D 抱歉帖子太短了。我扩展了它,现在问题应该更清楚了。感谢您的评论,我真的被困在这里,因为我真的想在这里使用照明。
-
这确实是预期的行为吗?