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 的 body 设置为 Activity 状态 - 这导致 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);
}
我收到最后一个错误,不是 fatal error [gdx-box2d64.dll+0xbd0d] 但 线程“LWJGL Application”中的异常 java.lang.ClassCastException: com.redsoft.game.Sprites.Items.ItemDef cannot被转换为 java.lang.Comparable
- 但在相同的情况下,所以它可能是同一件事,好吧。抱歉发了这么长的帖子,但我是 LibGDX 的新手。研究了两天没有解决。
请帮帮我。
最佳答案
你的 itemsToSpawn
Collection 于PlayScreen
是PriorityQueue<ItemDef>
.
在这里你试图将 ItemDef 对象添加到 itemsSpawn:
public void spawnItem(ItemDef idef) {
itemsToSpawn.add(idef);
}
但是PriorityQueue
只允许 Comparable
元素。
所以解决方案是将其更改为 LinkedBlockingQueue
就像在教程中一样。
关于java - Libgdx - 线程 "LWJGL Application"java.lang.ClassCastException 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48145725/