gpt4 book ai didi

java - 如何用java制作沙盒游戏

转载 作者:行者123 更新时间:2023-12-01 12:50:17 24 4
gpt4 key购买 nike

我正在尝试制作一款模拟化学 react 的游戏。问题是,大约五个灰尘颗粒后,游戏崩溃了。这是移动粒子的代码:

public class Element extends Thread {
long temperature;
double x;
double y;
double XVel;
double YVel = 5;
ImageView iv;
Duration dur;
TranslateTransition tt = new TranslateTransition();

@Override
public void run() {
while (true) {
move();// calls the movement method repeatedly
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(Element.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

public void initialize() {
this.start();
}

/**
* @return the temperature
*/
public long getTemperature() {
return temperature;
}

public void checkCollisions() {

}

public void move() {
tt.setDuration(dur.millis(5));
tt.setByY(YVel);
tt.setCycleCount(1);
tt.setNode(iv);
tt.play();
}
}

然后,出现此异常,游戏停止运行:

java.lang.NullPointerException
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:366)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:366)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:289)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:459)
at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:332)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)

我不知道为什么会发生 NullPointerException,任何人都可以帮助我(我正在使用 JavaFX)吗?如果有人能告诉我这个游戏是如何做到的( http://dan-ball.jp/en/javagame/dust/ ),那将会非常有帮助,因为这个游戏展示了我正在努力实现的目标。亲自看看。

最佳答案

我发现 TranslateTransition 的内存效率并不高。当我分析我的项目时,我注意到每当放置粒子时内存峰值都不会下降。

There are numerous issues with your code as others have pointed out, study this animated physics in JavaFX sample (https://gist.github.com/james-d/8327842) to see how you could achieve the behaviour you want. – jewelsea Jun 17 at 19:30

这个引用确实很有帮助。我现在真的应该能够完成我的游戏了。

关于java - 如何用java制作沙盒游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24271743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com