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

2014/2/2

Making the game continue

GreenAnthony GreenAnthony

2014/2/2

#
Okay I have made a mode in my game in which the player is able to make a level himself, of course this new data in the game needs to be saved somewhere. So I learnt how to create files and I am using this knowledge in my game. But people reported a small bug that happens when you create your own level, and then quit without letting it save properly. The game doesnt create all the files needed and then when the person tries to open up the game again it crashes (not only the game but sometimes also the computer!) in order to fix that I would like to know how to make it create all the needed files even if it is closed. Is that even possible or do I have to live with the bug? Can someone please tell me how to make the game continue to create the needed files even though the game itself is closed?
danpost danpost

2014/2/2

#
It sounds like you are keeping files open throughout the level creating mode. If you do all the file operations during a single act method, then they will need to complete before the scenario can be stopped (unless the power to the computer is turned off or lost without working battery back-up).
GreenAnthony GreenAnthony

2014/2/3

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
                try
    file = new BufferedWriter(new FileWriter("Name"+".java" )); 
    file.write("Whatever it needs to say"); 
    file.close(); 
catch (IOException ioe) { 
    ioe.printStackTrace(); 
finally
    try
        file.close(); 
    
    catch (IOException ioe) { 
        ioe.printStackTrace(); 
    
}
This is the code I use, how can I make it do it when the game is closed!
danpost danpost

2014/2/3

#
Use the 'stopped' method of the World class. See the documentation for further info.
GreenAnthony GreenAnthony

2014/2/3

#
Sorry but I don't really get how to use it, could you show me using the code I posted earlier?
danpost danpost

2014/2/3

#
'stopped' is a World class method; so, add a 'public void stopped()' method to your world class. Put the code you want to execute when the scenario is stopped in the method block. You do not have to programmatically call this method as it will execute automatically anytime the scenario is 'Paused' or the 'Greenfoot.stop()' method is encountered (provided the active world is an instance of that World subclass you placed the method in).
GreenAnthony GreenAnthony

2014/2/4

#
Okay I tried it, it doesn't really work!
You need to login to post a reply.