简体   繁体   English

Java Area.intersects(Rectangle)超出范围

[英]Java Area.intersects(Rectangle) out of bounds

im pretty new to programming so this might be really obvious, but when i run this part of my code 我是编程新手,所以这可能真的很明显,但是当我运行这段代码时

public void movePlayer(){
    Rectangle playerRect = new Rectangle((int) player.getxPositie(),(int) player.getyPositie()-12,20,35);

    if(water.intersects(playerRect)){//out of bounds????
        player.swim=true;
        jump=true;
        if(SplashP == true){
                SplashP=false;
                Splash((int) (player.getxPositie()/5),versnelling*12);
        }else{}

    }else{  player.swim=false;
            SplashP = true;}
        }
...

i get the following Exception and i don't know why 我收到以下异常,我也不知道为什么

Exception in thread "Thread-20" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
    at java.util.Vector.get(Vector.java:748)
    at java.awt.geom.Area.getCachedBounds(Area.java:434)
    at java.awt.geom.Area.intersects(Area.java:606)
    at java.awt.geom.Area.intersects(Area.java:618)
    at Model.movePlayer(Model.java:723)
    at Animator.run(Animator.java:19)
    at java.lang.Thread.run(Thread.java:745)

Another strange thing is that when the water intersects the player the else case alse gets triggerd half of the time 另一个奇怪的事情是,当水与玩家相交时,其他情况也会触发一半的时间

PS. PS。 Area water is build from a collection of triangles 区域水由一系列三角形构成

I think that this intersects function does not allow for Rect input. 我认为此相交函数不允许Rect输入。 I also advice you to make sure your code is in one language. 我还建议您确保您的代码使用一种语言。 I happen to know what your variables do because I am Dutch myself, but for the sake of consistency as well as everyone being able to read your code I advice you to make everything in English. 我碰巧知道您的变量是做什么的,因为我本人是荷兰人,但是为了保持一致性以及每个人都可以阅读您的代码,我建议您使用英语制作一切。

If you take a look at java.awt.geom.Area.getCachedBounds line 433-434 (see your stacktrace) then you find 如果您查看java.awt.geom.Area.getCachedBounds第433-434行(请参见stacktrace),则会发现

private Rectangle2D getCachedBounds() {
    ...
    if (curves.size() > 0) {                 // 433
        Curve c = (Curve) curves.get(0);     // 434

curves is a member of Area of type Vector . curvesVector类型Area的成员。 The code checks if curves is not empty and then accesses the first element. 该代码检查curves是否不为空 ,然后访问第一个元素。 This then raises an ArrayIndexOutOfBoundsException which can only happen when curves is empty. 然后,这将引发ArrayIndexOutOfBoundsException ,该ArrayIndexOutOfBoundsException只能在curves 空时发生。

So there must be another thread which modifies the water object in between. 因此,必须有另一个线程可以修改其间的water对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM