【问题标题】:Libgdx - Exception in thread "LWJGL Application" java.lang.ClassCastExceptionLibgdx - 线程“LWJGL 应用程序”java.lang.ClassCastException 中的异常
【发布时间】:2018-06-17 03:53:25
【问题描述】:

https://gist.github.com/Redas17/539382e654fe13613f70cf21cf515f22 - 这是我编写的所有类的链接。

您好,我需要您的帮助,我正在制作本教程中的马里奥游戏:https://www.youtube.com/watch?v=Z8g44JssVmc&t=3s

在视频 21 中,添加了一项功能,可以在击中硬币砖时生成蘑菇。一旦我添加了这个功能,游戏就开始了,但是在我用蘑菇击中硬币砖的 4 次中,有 1 次(大约)它崩溃了。我收到了这个错误

https://gist.github.com/Redas17/90d81231c16668e1ee6299ff13ad229b

然后我找到了一篇博文,他“找到”了解决方案。在这里——

今天,我终于有时间深入研究我的代码(因为我不使用 Brent 的代码 1:1,所以我喜欢在每节课后“整理”它以符合我自己的标准)并设法找到罪魁祸首。问题最终是PlayScreen.update() 试图将被破坏的 Goombas 的主体设置为活动 - 这导致 Box2d 引擎尝试访问不再可用的内存(Box2D 是用 C 编写的,只有一个很小的 ​​Java-Wrapper围绕它,所以它可以做到这一点)。解决方案是这样的: 在 PlayScreen.update() 而不是

if(enemy.getX() < player.getX() + 224 / MarioBros.PPM) {
                enemy.b2body.setActive(true);
            }

做类似的事情

if(!enemy.isDestroyed() && enemy.getX() < player.getX() + 224 / MarioBros.PPM) {
                enemy.b2body.setActive(true);
            }

为此,我们需要在 Enemy 中创建这个变量和方法:

protected boolean destroyed;

public boolean isDestroyed() {
                return destroyed
            }

并从 Goomba 和 Turtle 中删除变量“destroyed”。

你猜怎么着?问题仍然存在,但错误更改为 -

Exception in thread "LWJGL Application" java.lang.ClassCastException: com.redsoft.game.Sprites.Items.ItemDef cannot be cast to java.lang.Comparable
    at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:652)
    at java.util.PriorityQueue.siftUp(PriorityQueue.java:647)
    at java.util.PriorityQueue.offer(PriorityQueue.java:344)
    at java.util.PriorityQueue.add(PriorityQueue.java:321)
    at com.redsoft.game.Screens.PlayScreen.spawnItem(PlayScreen.java:80)
    at com.redsoft.game.Sprites.TileObjects.Coin.onHeadHit(Coin.java:36)
    at com.redsoft.game.Tools.WorldContactListener.beginContact(WorldContactListener.java:30)
    at com.badlogic.gdx.physics.box2d.World.beginContact(World.java:985)
    at com.badlogic.gdx.physics.box2d.World.jniStep(Native Method)
    at com.badlogic.gdx.physics.box2d.World.step(World.java:689)
    at com.redsoft.game.Screens.PlayScreen.update(PlayScreen.java:116)
    at com.redsoft.game.Screens.PlayScreen.render(PlayScreen.java:143)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.redsoft.game.MarioBros.render(MarioBros.java:48)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

有趣的是,当我在博文中评论行时

if (!enemy.isDestroyed() && enemy.getX() < player.getX() + 224 / MarioBros.PPM) {
                enemy.b2body.setActive(true);
            }

我得到了最后一个错误,不是致命错误 [gdx-box2d64.dll+0xbd0d] 而是 Exception in thread "LWJGL Application" java.lang.ClassCastException: com.redsoft.game.Sprites.Items.ItemDef cannot be cast to java.lang.Comparable - 但在相同的情况下,所以可能是同样的事情,好吧。对不起,很长的帖子,但我是 LibGDX 的新手。我研究了两天,没有任何解决办法。

请帮帮我。

【问题讨论】:

    标签: java android exception libgdx game-physics


    【解决方案1】:

    您在PlayScreen 中的itemsToSpawn 收藏是PriorityQueue&lt;ItemDef&gt;

    在这里您尝试将 ItemDef 对象添加到 itemsSpawn:

    public void spawnItem(ItemDef idef) {
         itemsToSpawn.add(idef);
    }
    

    但是PriorityQueue 只允许Comparable 元素。

    所以解决方案是将其更改为LinkedBlockingQueue,就像在教程中一样。

    【讨论】:

    • 谢谢你,你是我的英雄!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多