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

2012/2/14

using text files in a game

1
2
stevenGrohe stevenGrohe

2012/2/14

#
I made a game using textfiles (.txt) to have a easier level creation. Then I clicked "share", then "application", so i exported it to an .jar file. When I start this file, nothing happens. (It´s working in greenfoot though) Does that .jar file maybe not find the .txt file, because the location is different?? ! please answer :(
danpost danpost

2012/2/14

#
I had the same problem with image files I was using in a scenario I was writing for in-house use. You will have to have the .txt files in the same directory as the .jar and make sure that you save the jar file with the proper reference to the directory (I think I had to go up on directory level). You could use "../filename.txt", I think (of course, you replace 'filename' with the name of your file). Best, of course, would be to use the whole directory path to the filename, but the drawback of that, is that it will not be transportable.
stevenGrohe stevenGrohe

2012/2/14

#
thank you okay, and if I want to publish it in the gallery? Can I use text files then anymore?
danpost danpost

2012/2/14

#
I doubt it seriously. They are planning on having some storage per scenario, but it will be limited.
stevenGrohe stevenGrohe

2012/2/14

#
ah, I see.. okay thx
Duta Duta

2012/2/15

#
I also had a problem a few days ago - I'd made a quick alarm program to help me wake up, and to set the alarm time, there's a popup (which is in the world's constructor) which has some JComboBox's etcera to pick a time, and when you hit done it continues with the worlds constructor - anyway, it works perfectly in the Greenfoot environment, but if I export it as a jar, the pop-up is bugged - it is the correct popup (right size and title), but instead of buttons and combo-box'es its just blank, and to close the program you have to end the javaw.exe process in task manager...
Duta Duta

2012/2/15

#
Just went back to that scenario - turns out, if I make it so that instead of being called during the creation of the world, and instead do it in the first act cycle, it works... </confused>
danpost danpost

2012/2/15

#
I was using swing components also, and probably could very easily had similar problems, but I guess I avoided it by the way I had programmed it (sort of like a 'ready', 'set', and 'go' type). The act() asked for info at 'ready' where the swing components are used, processed the info and prepared the world at 'set' and then -- 'go'. This project was also to be used as a stand-alone .jar application.
darkmist255 darkmist255

2012/2/15

#
I'd be interested to know about saving level configurations in text files as well :D. @danpost If you happen to make any useful tools that you're only using as stand-alones, would you be able to upload them to the Gallery anyway? When it comes to learning from observing others' work, the more the merrier :D.
Builderboy2005 Builderboy2005

2012/2/15

#
You can definitely *read* text files in the gallery (I have hundreds of lines of text for the script for Nightmare) you just need to know how to go about accessing it. You have to go a roundabout way and use URL's to get you a buffered reader of the Text file.
URL path = getClass().getClassLoader().getResource("Text.txt");
InputStream input = path.openStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(input));
You can even access text files in sub-directories as long as you include the path in the file name. Writing to text files is not possible using Greenfoot Gallery, because of permission settings in applets.
danpost danpost

2012/2/15

#
@darkmist255, I only have the one right now and (a) it is not really a tool and (b) it is very specificified (if that is a word). What I mean is that it is to be used for a very specific purpose and would not be of much interest to other, except for maybe some of the code. I could at some point, re-vamp it and make it more generalized, but until then, it is solely in-house. As far as 'only one', I was referring to full-fledged applications (as I have dozens of test scenarios saved in my 'my blips' folder.
stevenGrohe stevenGrohe

2012/2/15

#
@builderbox2005 thank you, I already did the reading script, and it works well, but not if I make an .jar file. But As you say, uploading it to gallery is possible.
davmac davmac

2012/2/16

#
stevenGrohe, I think you've misunderstood Builderboy2005, he's giving you a way to read the script whether it is on the gallery or in a jar file.
darkmist255 darkmist255

2012/2/16

#
@danpost Ahhh, I know what you're talking about. I have a ton of very specific little nips of code that don't serve much purpose except for my specific machine :D.
stevenGrohe stevenGrohe

2012/2/16

#
davmac, okay, i´m a bit confused. To have totally clearness, here is my method which reads out the file for level creation, if this won´t work , please tell me why, and how it WILL work , when I load it up to the gallery:
    public void readLevel(String filename)
    {
        try
        {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(filename);
        
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
        
            //Read File Line By Line
            int i = 0;
            while ((strLine = br.readLine()) != null)
            {
                // Put the string of the line into object creation script
                if (i >= 3)
                loadLevelRow(strLine,i-3);
                else
                {
                    if (i == 0)
                    {
                        CONSTS.level_width = Integer.parseInt(strLine);
                    }
                    
                    if (i == 1)
                    {
                        CONSTS.level_height = Integer.parseInt(strLine);
                    }
                }
                
                i ++;
            }
        
            //Close the input stream
            in.close();
        }
        catch (Exception e)
        {
           //Catch exception if any
           System.err.println("Error: " + e.getMessage());
        }
    }
There are more replies on the next page.
1
2