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

2013/9/2

I want to display message on world.

savanibharat savanibharat

2013/9/2

#
I want to display message in world on event. I have used this. public class ImageLabel extends Actor { /** * Act - do whatever the ImageLabel wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public ImageLabel() { GreenfootImage g=new GreenfootImage(100,20); g.drawString("message",2,20); setImage(g); } public void act() { // Add your action code here. } } But this I do only manually. I want to do on the event. Event is one of the image disappears. This is my code for image disappearing Both of the below methods are in same class. public void act() { //other methods called. pennymoved(); } void pennymoved(){ Actor worldActorpenny = getOneIntersectingObject(Penny.class); if (worldActorpenny != null) { getWorld().removeObject(worldActorpenny); } }
SPower SPower

2013/9/2

#
Replace some part of your pennymoved method with this:
if (worldActorpenny != null) {
    getWorld().removeObject(worldActorpenny);
    getWorld().addObject(new ImageLabel(), x, y);
    // replace x and y with the coordinates you want
}
And I'd like to give you some advice for your ImageLabel class:
// change your constructor to:
public ImageLabel(String txt)
{
    // an image with the text of the String txt, size 20, black text color and transparent background
    // refer to the documentation for more information about this constructor
    GreenfootImage g = new GreenfootImage(txt, 20, null, null);
    setImage(g);
}
which allows you to specify the text of your label, by giving it as an argument. Like this:
new ImageLabel("This is a label")
for more info, go to the documentation: http://www.greenfoot.org/files/javadoc/
savanibharat savanibharat

2013/9/3

#
Thank you very much SPower. It worked.
SPower SPower

2013/9/3

#
You're welcome!
You need to login to post a reply.