javafx重新计算父/节点边界时如何修复IndexOutOfBounds异常

时间:2019-05-11 11:52:34

标签: java multithreading user-interface javafx bounds

我正在制作javafx gui应用程序/游戏,需要同时更新角色视图。每个移动周期我都会更新角色的ImageView,如下所示:

if (inBounds(direction) && !Collisions.collides(newHitbox, hitbox)) {
        this.setX(direction.getX() * this.getVelocity() + (int) this.view.getX());
        this.setY(direction.getY() * this.getVelocity() + (int) this.view.getY());
        this.hitbox.setX(direction.getX() * this.getVelocity() + this.hitbox.getX());
        this.hitbox.setY(direction.getY() * this.getVelocity() + this.hitbox.getY());
    }

我将Spritesheet用于运动的错觉,因此我每四个周期更新一次视口。

一段时间后,一切正常,但在某些时候我收到以下错误消息:

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89)
at com.sun.javafx.collections.VetoableListDecorator.get(VetoableListDecorator.java:306)
at javafx.scene.Parent.updateCachedBounds(Parent.java:1701)
at javafx.scene.Parent.recomputeBounds(Parent.java:1645)
at javafx.scene.Parent.doComputeGeomBounds(Parent.java:1498)
at javafx.scene.Parent.access$200(Parent.java:79)
at javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
at com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
at com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBoundsImpl(RegionHelper.java:78)
at com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBounds(RegionHelper.java:62)
at javafx.scene.layout.Region.doComputeGeomBounds(Region.java:3289)
at javafx.scene.layout.Region.access$300(Region.java:147)
at javafx.scene.layout.Region$1.doComputeGeomBounds(Region.java:168)
at com.sun.javafx.scene.layout.RegionHelper.computeGeomBoundsImpl(RegionHelper.java:89)
at com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:115)
at javafx.scene.Node.updateGeomBounds(Node.java:3837)
at javafx.scene.Node.getGeomBounds(Node.java:3799)
at javafx.scene.Node.updateBounds(Node.java:771)
at javafx.scene.Parent.updateBounds(Parent.java:1832)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2497)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:412)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:411)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:438)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:519)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:499)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:492)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:320)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run$$$capture(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
at java.base/java.lang.Thread.run(Thread.java:834)

因为这种情况不会持续发生,所以我假设线程在某个时刻尝试同时访问其ImageView的父级,并且出现了问题。

我无法定位错误,因为错误消息并没有告诉我在代码中发生此错误的地方。我试图使更新视图的方法以及与以Parent对象作为其监视器的代码部分同步的方法,但这根本没有帮助。

我个人不使用那些正在更新的边界,所以如果有可能关闭重新计算可能会这样做。

0 个答案:

没有答案