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


Looking for Suggestions on Level Loading

MrCohen MrCohen


Hello, I've been making my way through Greenfoot, learning as I go, and creating some pretty cool scenarios along the way. So far, in order to present the user with multiple game levels, I have been using a crude system that uses an if statement to load appropriate assets. For example: public void levelLoader (int level) if (level == 1) { someArrayOfActors= new mobArray; someArrayOfActors = new Monster("Some", "Random", 123, "Constructor Call Fields"); // etc. } Overall, it works, but I can't help feeling that I'm producing very crude code. My understanding is that there is no file system available from Greenfoot. In my first project, I added the level loader as a method in my world. For my second project, I'm contempating a Level class that would house the crude code, load the levels, and then pass a few arrays of objects back to my world to store and process. This will be quite a little coding task, so I thought I would ask for input before I got started. Many thanks in advance :)
davmac davmac


It's true that no Java applet can access the file system as such, unless the applet is signed (which is not usually viable for Greenfoot applets). Greenfoot programs can however access the file system if they run locally (either from within Greenfoot itself, or when they exported as a stand-alone application). Also, applets can load resources in a manner similar to reading files. The technique is explained in some detail here: (the text concerns BlueJ, but the same applies for Greenfoot). The critical part is the code:
public InputStream openFile(String fileName) 
    throws IOException
    URL url = getClass().getClassLoader().getResource(fileName);
    if(url == null)
        throw new IOException("File not found: " + fileName);
    return url.openStream();
MrCohen MrCohen


Thanks I'll check that out.
You need to login to post a reply.