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

4 days ago

Adding text and image and then make it disappear.

BoondockSaint BoondockSaint

4 days ago

#
Hello!, so i have made a brick breaker game that i saw on YouTube and I want when i click play before the ball starts moving, to have an image(of a character) appear and next to it a text that will disappear after a few seconds. Can anyone please help me? i currently have made in a separate world to test it this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class cannot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class cannot extends Actor
{
    /**
     * Act - do whatever the cannot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
{
    setImage(new GreenfootImage("You cannot defeat me...!", 30, Color.WHITE, new Color(0,0,0,0)));
    Greenfoot.playSound("cannot.mp3");
    Greenfoot.delay(100);
    Greenfoot.stop();
    
}
       
}
and it just put the image and the text there and then they just stay there.
danpost danpost

4 days ago

#
Change line 16 to;
protected void addedToWorld(World world)
and then line 21 to:
world.removeObject(this);
BoondockSaint BoondockSaint

4 days ago

#
thank you! but now they don't appear, should i add a sentence with coordinates?
danpost danpost

4 days ago

#
BoondockSaint wrote...
thank you! but now they don't appear, should i add a sentence with coordinates?
What does not disappear? Shoe revised code.
BoondockSaint BoondockSaint

4 days ago

#
i mean that now i have the opposite problem. whereas before the image and the text wouldn't disappear after a while now they don't show up at all. the code is that now. that's the code for the text:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class cannot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class cannot extends Actor
{
    /**
     * Act - do whatever the cannot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void addedToWorld(World world) 
{
    setImage(new GreenfootImage("You cannot defeat me...!", 30, Color.WHITE, new Color(0,0,0,0)));
    Greenfoot.playSound("cannot.mp3");
    Greenfoot.delay(100);
    world.removeObject(this);
    
}
       
}
that's the code for the image(the robot):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class robot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class robot extends Actor
{
    /**
     * Act - do whatever the robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void addedToWorld(World world)
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() -320, image.getHeight() -320);
        setImage(image);
        world.removeObject(this);
    }
    public void act() 
    {
        // Add your action code here.
    }    
}
and lastly that's the code of the world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); 
        prepare();
    }
    
    public void prepare()
    {
        getBackground().setColor(Color.BLACK);
        getBackground().fill();
        
        robot robot = new robot();
        addObject(robot,549,201);
        robot.setLocation(236,316);
        robot.setLocation(172,340);
        
        cannot cannot = new cannot();
        addObject(cannot,390,302);
    }
}
danpost danpost

4 days ago

#
BoondockSaint wrote...
i mean that now i have the opposite problem. whereas before the image and the text wouldn't disappear after a while now they don't show up at all. the code is that now.
Remove line 20 from robot class. Place at line 22 in cannot class the following line:
world.removeObjects(world.getObjects(robot.class));
BoondockSaint BoondockSaint

17 hours ago

#
You need to login to post a reply.