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

2014/5/20

Changing text

1
2
mandyg233 mandyg233

2014/5/20

#
So I'm not really sure how to go about this, but I'm trying to figure out how to change the text of a label if different things happen (like if the boat moves to a certain location, etc). How would I change the text of a label like this? I'm going to try to have at least 10 different phrases in the text. Thank you!
mandyg233 mandyg233

2014/5/20

#
This is what I have so far: import greenfoot.*; import java.awt.Color; public class Label extends Actor { public Label() { updateImage("Oh no!"); } public Label(String text) { updateImage(text); } public void setText(String text) { updateImage(text); } private void updateImage(String text) { setImage(new GreenfootImage(text, 90, Color.BLACK, new Color(0,0,0,0) )); } }
danpost danpost

2014/5/20

#
Where is the code of the class in which you are changing the text of the Label object from?
mandyg233 mandyg233

2014/5/20

#
I'm not sure, should it all be in the Label class? Boat class?
mandyg233 mandyg233

2014/5/20

#
Would I need different classes with each of them as different labels?
mandyg233 mandyg233

2014/5/20

#
Also, how do I remove the label?
danpost danpost

2014/5/20

#
mandyg233 wrote...
I'm not sure, should it all be in the Label class? Boat class?
Right now, I only know of you using the label object in the Boat class.
mandyg233 wrote...
Would I need different classes with each of them as different labels?
No.
mandyg233 wrote...
Also, how do I remove the label?
'getWorld().removeObject(/* label reference */);'. But, it may depend on which class your removal code might be in.
mandyg233 mandyg233

2014/5/20

#
I think I'll be using it in a different class as well, the Sled class. I tried getWorld().removeObject(new Label1()); and it didn't go away
mandyg233 mandyg233

2014/5/20

#
The label is initialing being created by the Boat class if that helps
danpost danpost

2014/5/20

#
mandyg233 wrote...
I tried getWorld().removeObject(new Label1()); and it didn't go away
No. creating a new object and trying to remove it before adding that label to the world will not work; nor will it remove the label object that is already in the world. You may want to keep the Label object in the world regardless of whether text is showing or not. Since it has a transparent background to the text, if you set its text to an empty String, 'setText("")', it will be invisible and ready to show new text at any time. You can add it to the world in your world constructor, like any other object you add at load time. And you can keep a reference to it in the world in a 'public static' field so that any class can change its text easily with '/* world name */.label.setText("new text");'.
mandyg233 mandyg233

2014/5/20

#
where do you access the public static field thing?
danpost danpost

2014/5/20

#
You would put it in your world class as a class field:
1
2
3
4
5
6
7
8
9
10
11
12
public class WorldName extends World
{
    public static Label label = new Label("");
 
    public WorldName()
    {
        super(/* world dimensions */);
        addObject(label, /* location coordinates */);
        // etc.
    }
    // etc.
}
mandyg233 mandyg233

2014/5/20

#
Sweet! Thank you so much
mandyg233 mandyg233

2014/5/20

#
What would I do if I wanted the text to be able to go onto two or possibly three lines?
mandyg233 mandyg233

2014/5/20

#
Also, I keep getting this heap space error
There are more replies on the next page.
1
2