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

2014/5/17

Making a string disappear from screen

cobarr cobarr

2014/5/17

#
In my code below i have left if mouse it clicked, the __ blank because I am not sure how to make this string of text or I suppose actor, go away once it has been clicked. Can anyone tell me what I should put or use to make the actor/sting disappear once it has been clicked on by the mouse?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

public class Instructions extends Actor
{
    // Create image with text. Text is in white with black image background.
    
    private Font textFont = new Font("TimesRoman", Font.BOLD,18);
    
    public Instructions()
    {     
        String longString = "some long text"
                 + System.getProperty("line.separator") +" some more long text";
                  
       setImage(new GreenfootImage( longString , 18, Color.WHITE, Color.BLUE));
       
       if (Greenfoot.mouseClicked(this)){
        
        
        }
     
    
    }
}
danpost danpost

2014/5/17

#
The World class has the method for removing actors from a world. You can use the Actor class method 'getWorld' to get the world the actor is in from the class of the actor. Therefore,
getWorld().removeObject(this);
would remove 'this' actor from the world.
cobarr cobarr

2014/5/17

#
Hi danpost, Thank you for your response. I have added this and when compiling there are no errors, however when I drag this "Instructions" Actor and then click on it, it doesn't get removed.
public class Instructions extends Actor
{
    // Create image with text. Text is in white with black image background.

    private Font textFont = new Font("TimesRoman", Font.BOLD,18);

    public Instructions()
    {     
        String longString = "some long text"
            + System.getProperty("line.separator") +" some more long text";

        setImage(new GreenfootImage( longString , 18, Color.WHITE, Color.BLUE));

        if (Greenfoot.mouseClicked(this)){
            getWorld().removeObject(this);
        }
    
    }
}
danpost danpost

2014/5/18

#
Lines 14 and 15 will not work in the constructor method. It needs to be in the act method where it will be constantly checked on.
cobarr cobarr

2014/5/18

#
Thank you! It works :)
You need to login to post a reply.