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

2014/5/18

Long Strings

cobarr cobarr

2014/5/18

#
The code below is working, but I would like to add multiple line breaks in the "longString" string. I tried adding another + System.getProperty("line.separator") + "text", but it doesn't work. The goal here is to create one image that contains text with breaks/new paragraphs so that text is not all clumped together. Once all the text is displayed under one actor, when the actor is placed into the world, I would like it to automatically get centered on the game screen.
1
2
3
4
5
6
7
8
public Instructions()
    {    
        String longString = "Gameplay:"
            + System.getProperty("line.separator") + "If your answer is correct, one point to the Counter will be added,";
                   
            setImage(new GreenfootImage( longString , 18, Color.WHITE, Color.BLUE));
 
    }
danpost danpost

2014/5/18

#
You can use the 'new line' escape sequence ( '\n' ) within the string to start a new line. For example:
1
2
3
String longString = "Gameplay:\n\nIf your answer is correct, one point to the Counter will be added,";
longString += "\nif your answer is not correct, one point to the Counter will be subtracted.";
setImage(new Greenfootimage(longString, 18, Color.white, Color.blue));
cobarr cobarr

2014/5/18

#
Ah! Great, thanks for introducing me to the 'new line' escape sequence ('\n')!
You need to login to post a reply.