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

2018/4/9

help with Text

student1 student1

2018/4/9

#
i'm making a game and i want to add text into the game so when it goes to the game over screen there will be text that the player can click on in order to rest the game and continue playing
danpost danpost

2018/4/9

#
student1 wrote...
i'm making a game and i want to add text into the game so when it goes to the game over screen there will be text that the player can click on in order to rest the game and continue playing
The basics would be an actor to display the text -- a basic Actor subclass class is:
public class SimpleActor extends greenfoot.Actor {}
-- and the following in your World subclass:
// a field to hold the actor
private Actor resetActor;

// a game over method with the following to apply the reset actor
GreenfootImage resetText = new GreenfootImage("Click here to play again", 48, Color.BLACK, new Color(0, 0, 0, 0));
GreenfootImage resetImage = new GreenfootImage(resetText.getWidth()+30, resetText.getWidth()+20);
resetImage.setColor(Color.RED);
resetImage.fill();
resetImage.setColor(Color.LIGHT_GRAY);
resetImg.fillRect(3, 2, resetImage.getWidth()-6, resetImage.getHeight()-4);
resetImage.drawImage(resetText, 15, 10);
resetActor = new SimpleActor();
resetActor.setImage(resetImage);
addObject(resetActor, getWidth()/2, getHeight()/2);

// and finally, in act method
if (resetActor != null && Greenfoot.mouseClicked(resetActor)) Greenfoot.setWorld(new MyWorld());
All the imaging from line 6 through to line 11 is not needed, but makes it look better (without which line 13 can set resetText as the image for resetActor).
student1 student1

2018/4/9

#
on line 10 the variable resetImg what would that be defined as
Navique Navique

2018/4/9

#
student1 wrote...
on line 10 the variable resetImg what would that be defined as
seems that it is defining itself, a little earlier above.
danpost danpost

2018/4/9

#
student1 wrote...
on line 10 the variable resetImg what would that be defined as
I think it obvious that it should have been resetImage.
student1 student1

2018/4/9

#
ok i tried doing the code differently but it doesn't work
public class Text extends Actor
{    
    public void act() 
    {
        GreenfootImage resetText = new GreenfootImage("Play again", 48, Color.BLACK, new Color(0, 0, 0, 0));        
        if(Greenfoot.mouseClicked(resetText))
        {
            Greenfoot.setWorld(new Start());
        }
    } 
}

public class GameOver extends World
{
    public GameOver()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); 
        
        Add();
    }
    
    public void Add()
    {
        Text text = new Text();
        addObject(text,450,450);
    }
}
danpost danpost

2018/4/9

#
student1 wrote...
ok i tried doing the code differently but it doesn't work << Code Omitted >>
Try this:
public class Text extends Actor
{
    public Text()
    {
        GreenfootImage resetText = new GreenfootImage("Play again", 48, Color.BLACK, new Color(0, 0, 0, 0));
        setImage(resetText);
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(this)) Greenfoot.setWorld(new Start());
    }
}
student1 student1

2018/4/9

#
thank you it works perfectly
You need to login to post a reply.