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

2021/6/10

Adding text and image and then make it disappear.

BoondockSaint BoondockSaint

2021/6/10

#
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

2021/6/10

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

2021/6/10

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

2021/6/10

#
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

2021/6/10

#
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

2021/6/10

#
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

2021/6/14

#
Gabe1098 Gabe1098

2021/6/15

#
you can make a text like this
showImage(new GreenfootImage("Text", 24, Color.BLACK, Color.WHITE));
danpost danpost

2021/6/16

#
BoondockSaint wrote...
still nothing just the black background.
I think the problem was that you were trying to use delay before the scenario starts running. Try this in your world:
public void started()
{
    if (getObjects(cannot.class).size() !- 0)
    {
        Greenfoot.playSound("cannot.mp3");
        Greenfoot.delay(120);
    }
}
Then move the removal lines to the act methods in robot and cannot.
BoondockSaint BoondockSaint

2021/7/1

#
it gives me an error at the ! symbol saying "not a statement" and after the ) "needs an ;" also by removal lines you mean the "remove.object"?
BoondockSaint BoondockSaint

2021/7/1

#
Gabe1098 wrote...
you can make a text like this
showImage(new GreenfootImage("Text", 24, Color.BLACK, Color.WHITE));
Also thank you. I will try that as well if i still have problems with what I'm doing horribly right know.
danpost danpost

2021/7/1

#
BoondockSaint wrote...
it gives me an error at the ! symbol saying "not a statement" and after the ) "needs an ;"
Yeah, I miss-typed a "-" where a "=" should be on line 3.
by removal lines you mean the "remove.object"?
Correct -- using "getWorld()" instead of "world". As far as the line provided by Gabe1098, there is no known method called showImage.
BoondockSaint BoondockSaint

2021/7/3

#
I am a little confused about the removal lines. What exactly should i write?
danpost danpost

2021/7/3

#
BoondockSaint wrote...
I am a little confused about the removal lines. What exactly should i write?
This should work for robot class:
import greenfoot.*;

public class robot extends Actor
{
    public robot()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() -320, image.getHeight() -320);
        setImage(image);
    }
    
    public void act() 
    {
        getWorld().removeObject(this);
    }    
}
Use something similar for cannot class.
Gabe1098 Gabe1098

2021/7/3

#
BoondockSaint wrote...
Gabe1098 wrote...
you can make a text like this
showImage(new GreenfootImage("Text", 24, Color.BLACK, Color.WHITE));
Also thank you. I will try that as well if i still have problems with what I'm doing horribly right know.
oops I'm sorry I should have put setImage instead of showImage
You need to login to post a reply.