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

2019/8/14

Scenario not showing properly when I share it here

protocolUnknown protocolUnknown

2019/8/14

#
I shared a scenario but it is not showing properly. All the objects are showing like normal but it is not displaying the text objects I had put into the game. The text objects do this in its constructor method: GreenfootImage gi= new GreenfootImage( "some string", 20, Color.WHITE, Color.BLACK); setImage(gi); I'm not sure if that's ^ the issue, but it ad everything else is working fine on my Greenfoot editor.
protocolUnknown protocolUnknown

2019/8/14

#
Actually, I found what the problem is, but I don't know how to fix it. Those Text objects display text that is read from a txt file that I put in the project folder, but on here when I publish it, I guess it doesn't look at the txt file or it didn't package with it.
danpost danpost

2019/8/14

#
protocolUnknown wrote...
Actually, I found what the problem is, but I don't know how to fix it. Those Text objects display text that is read from a txt file that I put in the project folder, but on here when I publish it, I guess it doesn't look at the txt file or it didn't package with it.
I kind of doubt that is the case. However, I was going to take a look at your scenario; but, when you last updated it, you forgot to unlock it.
protocolUnknown protocolUnknown

2019/8/14

#
danpost wrote...
protocolUnknown wrote...
Actually, I found what the problem is, but I don't know how to fix it. Those Text objects display text that is read from a txt file that I put in the project folder, but on here when I publish it, I guess it doesn't look at the txt file or it didn't package with it.
I kind of doubt that is the case. However, I was going to take a look at your scenario; but, when you last updated it, you forgot to unlock it.
i unlocked it now
danpost danpost

2019/8/14

#
protocolUnknown wrote...
i unlocked it now
No. It is still locked.
protocolUnknown protocolUnknown

2019/8/14

#
danpost wrote...
protocolUnknown wrote...
i unlocked it now
No. It is still locked.
Do you mean also publish the source code? I unlocked it again.
danpost danpost

2019/8/14

#
protocolUnknown wrote...
Do you mean also publish the source code?
Yes. Cannot do anything with it, otherwise. ( still locked unpublished at this time) Oh, that's right -- unlocking just allows single-frame "act"ing (at least, it used to) and access to speed slider.
protocolUnknown protocolUnknown

2019/8/14

#
danpost wrote...
protocolUnknown wrote...
Do you mean also publish the source code?
Yes. Cannot do anything with it, otherwise. ( still locked unpublished at this time) Oh, that's right -- unlocking just allows single-frame "act"ing (at least, it used to) and access to speed slider.
Ok, I published it but I'll just warn you now that it is a bit of a messy code. The method that reads txt files is called readTxtFile and is in world class.
danpost danpost

2019/8/14

#
protocolUnknown wrote...
Ok, I published it but I'll just warn you now that it is a bit of a messy code. The method that reads txt files is called readTxtFile and is in world class.
Got it. Will look at it shortly.
danpost danpost

2019/8/14

#
I think the problem is the way you are reading the text file. You do it in way that is not compatible with the javascript conversion. I believe I saw a line with an InputStream object being created; but, it (the object) was not being used. At any rate, on general principles, you should only access your file once and read the entire thing into memory (a List object or a String array would suffice). The following method should suit your needs:
/**
    Return the lines of the given text file as an array of String objects.
    
    @param filename the name of the file to be read
    
    @return String array of file contents or null if file not found
*/
public static String[] getFileText(String filename)
{
    java.util.List<String> lines = new java.util.ArrayList<String>();
    BufferedReader br = null; // sets up a local field for the BufferedReader object
    // attempt to open an input stream to the file given
    try
    {
        InputStream input = getClass().getClassLoader().getResourceAsStream(filename); // open stream
        br = new BufferedReader(new InputStreamReader(input)); // wrap it within a BufferedReader object
    }
    catch (Exception e) { System.out.println("'"+filename+"' file not found"); return (String[]) null; } // for failure to open file
    // attempt to read in the lines of text
    try
    {
        String line = null; // sets up a local field for each line of text
        while ((line = br.readLine()) != null)  lines.add(line); // read each line and add them to the 'lines' list
        br.close(); // close the BufferedReader object
    }
    catch (Exception e) { try { br.close(); } catch (Exception f) {} } // close file if read error occurred
    return lines.toArray(new String[0]);
}
protocolUnknown protocolUnknown

2019/8/15

#
Ah, thanks a ton. I didn't know that JS would play into this as a factor but now I know. The scenario is now working properly and is, in fact, more efficient because it isn't re-reading the txt file over and over again like it had to before.
You need to login to post a reply.