1
votes

https://gist.github.com/Redas17/539382e654fe13613f70cf21cf515f22 - That is a link to all the classes I have coded.

Hello I need your help, I am working on a Mario game from this tutorial: https://www.youtube.com/watch?v=Z8g44JssVmc&t=3s

On video 21 a feature was added to spawn mushrooms whenever a coin brick was hit. As soon as I added this feature, the game starts, but in 1 of 4 times (about) it's crashes when I hit coin brick with mushroom. I was getting this error

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

Then I found a blogpost, who "found" solution. Here it is -

Today, I finally had the time to dig into my code (since I don't use Brent's code 1:1, I like to "tidy it up" after each lesson so it adheres to my own standards) and managed to find the culprit. The problem was in the end that PlayScreen.update() tried to set the bodies of destroyed Goombas active - that caused the Box2d engine to try to access memory that is not available anymore (Box2D is written in C, there is just a tiny Java-Wrapper around it so it can do that).The solution is something like this: In PlayScreen.update() instead of

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

do something like

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

For that to work we need to create this variable and method in Enemy:

protected boolean destroyed;

public boolean isDestroyed() {
                return destroyed
            }

and remove the variable "destroyed" from Goomba and Turtle.

And guess what? Problem still there, but error changed to -

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)

Interesting that when I comment lines from the blogpost

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

I am getting last error, not fatal error [gdx-box2d64.dll+0xbd0d] but Exception in thread "LWJGL Application" java.lang.ClassCastException: com.redsoft.game.Sprites.Items.ItemDef cannot be cast to java.lang.Comparable - but in the same circumstances, so maybe it's kind of the same thing, well. Sorry for long post, but I am new into LibGDX. I have researched for two days without any solution.

Please help me.

1

1 Answers

1
votes

Your itemsToSpawn collection in PlayScreen is PriorityQueue<ItemDef>.

here you are trying to add ItemDef object to itemsSpawn:

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

But PriorityQueue allows only Comparable elements.

So solution is change it to LinkedBlockingQueue like in tutorial.