首页 > 解决方案 > javafx重新计算父/节点边界时如何修复IndexOutOfBounds异常

问题描述

我正在制作一个 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 对象同步的代码部分作为它们的监视器,但这根本没有帮助。

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

标签: javamultithreadinguser-interfacejavafxbounds

解决方案


推荐阅读