This site requires JavaScript, please enable it in your browser!
Greenfoot back
Vesuv
Vesuv wrote ...

2018/9/26

Exported to jar - Game freezes when changing World

Vesuv Vesuv

2018/9/26

#
Hi everyone, I created a little project a few days ago, now I wanted to export it as .JAR to share with some friends. However when I tested the Jar I was able to "play", but when the GameOver should have been reached (either <=0 HP or last enemy) the Game should (directly, got before a little transition) however, whenever the game should switch the World the game freezes / stopps. (With Controls you can see that the "play Button" returned to its beginning icon. The Call I made was in the World class which was the active Playground. I made previously (reason was the transition) a method which just catches the call if the game was won or lost:
public void ending(boolean won){
this.won = won;
finalEnding();
}

public void finalEnding(){
if(won==true){
Greenfoot.setWorld(new EndScore(getScore()));
}else{
Greenfoot.setWorld(new Lost(getScore()));
}}
--- more code ---
public int getScore(){
return this.score
}
It is necessary to send an int to the world since it is the score which shall be shown. I hope you can help me and get slight Idea what my problem is. with best regards vesuv
Super_Hippo Super_Hippo

2018/9/26

#
You split it up into three methods for no reason, but other than that, I don't see a problem. (Well, there is a semicolon missing in line 14, but I guess Greenfoot would have noticed that.) This one line should do the same thing as your three methods:
public void ending(boolean won)
{
    Greenfoot.setWorld(won ? new EndScore(score) : new Lost(score));
}
Vesuv Vesuv

2018/9/26

#
Unfortunatly this did not solve the problem and as I stated in the text above I had seperated it, due to a transition which I for now excluded. Maybe I should add a few try and catch... As I said the Problem is: In Greenfoot Environment it works, however when I export it (either JAR or to the Greenfort page) it freezes at the moment when it should change the World. Edit: I found the Problem. stupid me. I had a mistake in a different class, which I was sure would work 100%. Never trust your writing!
        this.setBackground("menu/youWon.png"); //incorrect!
        this.setBackground("Menu/youWon.png"); //correct
Vesuv Vesuv

2018/9/26

#
Ok I "tested" some more and it seems like the problem is to change the world. I attached this time a picture in which you can see the different result after reaching the event Lost. It is the same code!
    public void ending (boolean won){
        this.getBackground().setColor(new Color(255,0,0));
        this.getBackground().setFont(new Font(true, true, 48));
        this.getBackground().drawString("NonError before won = won!", 50, 100);
        this.won = won;
    /**
       * In here stands additional code which would enable the transition.
       * This is the reason why there is the method "ending".
       */
        this.getBackground().setColor(new Color(0,255,0));
        this.getBackground().setFont(new Font(true, true, 48));
        this.getBackground().drawString("NonError after won = won!", 50, 160);
    
    finalEnding(); 
    
    }

    public void finalEnding(){
        
        this.getBackground().setColor(new Color(255,255,255));
        this.getBackground().setFont(new Font(true, true, 48));
        this.getBackground().drawString("NonError before changing World!", 50, 300);
        if (won == true){
EndScore(getScore()));
        Greenfoot.setWorld(new EndScore2());
        }
        else{
        Greenfoot.setWorld(new Lost2());        
    
    }}


Do I need to do something like this?
try{
 if (won == true){
EndScore(getScore()));
        Greenfoot.setWorld(new EndScore2());
        }
        else{
        Greenfoot.setWorld(new Lost2());        
}
        catch (Exception e) //or something alike
       {*do Error message*}


danpost danpost

2018/9/27

#
Vesuv wrote...
Ok I "tested" some more and it seems like the problem is to change the world.
You do not need a try-catch structure for this coding. You probably do not have your Lost2 and EndScor2 class constructors excepting a score value. For example, in Lost2 class:
public Lost2(int score)
Upon fixing that, use the line Hippo supplied.
Vesuv Vesuv

2018/10/7

#
Vesuv wrote...
I found the Problem. stupid me. I had a mistake in a different class, which I was sure would work 100%. Never trust your writing!
        this.setBackground("menu/youWon.png"); //incorrect!
        this.setBackground("Menu/youWon.png"); //correct
But thanks. :)
You need to login to post a reply.