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

2025/6/10

pause, resume, and exit button

no_name2 no_name2

2025/6/10

#
due to my other account called no_name being stuck with a pending request, i couldn't send any more discussions on that account until the devs reviewed it so i made a new account to asks questions. i am having an issue with removing the exit button when the resume button is clicked. here is the code: pause button code: import greenfoot.*; public class Pause extends Actor { public Pause(String image) { setImage(new GreenfootImage(image)); } public void act() { if (Greenfoot.mouseClicked(this)) { Map1.isPaused = true; Resume resume_button = new Resume("resume.png"); getWorld().addObject(resume_button,675,380); ExitG e1 = new ExitG("exit.png"); getWorld().addObject(e1,675,450); getWorld().removeObject(this); //Greenfoot.setWorld(new Play()); // create world only when clicked } } } resume button code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Resume here. * * @author (your name) * @version (a version number or a date) */ public class Resume extends Actor { /** * Act - do whatever the Resume wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Resume(String image) { setImage(new GreenfootImage(image)); } public void act() { if (Greenfoot.mouseClicked(this)) { Map1.isPaused = false; Pause pause_button = new Pause("pause.png"); getWorld().addObject(pause_button,100,50); getWorld().removeObject(this); } } } exit button code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Exit here. * * @author (your name) * @version (a version number or a date) */ public class ExitG extends Actor { public ExitG(String image) { setImage(new GreenfootImage(image)); } public void act() { if (Greenfoot.mouseClicked(this)) { Greenfoot.setWorld(new Play()); getWorld().removeObject(this); } } } how do i make it so both the resume button and exit button disappear when the resume button is clicked.
Philipe_Manor Philipe_Manor

2025/6/11

#
Hello friend, check if this works:
import greenfoot.*;

public class Resume extends Actor
{
    public Resume(String image) {
        setImage(new GreenfootImage(image));
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(this)) {
            Map1.isPaused = false;

            // You must remov the resume button (this)
            getWorld().removeObject(this);

            // Remove all ExitG buttons from the world
            for (ExitG exit : getWorld().getObjects(ExitG.class)) {
                getWorld().removeObject(exit);
            }

            // Add the pause button back
            Pause pause_button = new Pause("pause.png");
            getWorld().addObject(pause_button, 100, 50);
        }
    }
}
danpost danpost

2025/6/11

#
Line 6 is missing a few parameters (font size, text color and background color). Lines 18, 19 and 24 all attempt to get the World object using getWorld() when the returned value is in fact null (due to line 15 removing the actor from the world). I would not write a class for each button. I would just keep a reference to the button actors in fields and have the world control what they do and when they appear. With the following (one-line) class:
public class Aktor extends greenfoot.Actor {}
you can have something like the following in your world:
private Actor btnResume, btnPause, btnExit;

public MyWorld() {
    super(600, 400, 1);
    btnResume = getButtonActor("RESUME", Color.BLACK, Color..GREEN);
    btnPause = getButtonActor("PAUSE", Color.BLACK, Color.YELLOW);
    btnExit = getButtonActor("EXIT", Color.BLACK, Color.CYAN);
}

private Actor getButtonActor(String text, Color textColor, Color backgroundColor) {
    Actor button = new Aktor();
    GreenfootImage buttonImage = new GreenfootImage(120, 30);
    buttonImage.setColor(backgroundColor);
    buttonImage.fill();
    buttonImage.setColor(Color.BLACK);
    buttonImage.drawRect(0, 0, 119, 29);
    GreenfootImage textImage = new GreenfootImage(text, 26, textColor, new Color(0, 0, 0, 0));
    buttonImage.drawImage(textImage, (buttonImage.getWidth()-textImage.getWidth())/2, 2);
    button.setImage(buttonImage);
    return button;
}

public void act() {
    if (Greenfoot.mouseClicked(btnResume) {
        removeObject(btnExit);
        removeObject(btnResume);
        addObject(btnPause, /** wherever */);
    }
    if (Greenfoot.mouseCLicked(btnPause) {
        removeObject(btnPause);
        addObject(btnExit, /** wherever */);
        addObject(btnResume, /** wherever */);
    }
}
(code is not complete { all private stuff is, as far as it goes }, but should get you going)
danpost danpost

2025/6/11

#
In fact, the act method could be refactored:
public void act() {
    if (Greenfoot.mouseClicked(btnResume)) resumeGame();
    if (Greenfoot.mouseClicked(btnPause)) pauseGame();
}

private void resumeGame() {
    removeObject(btnExit);
    removeObject(btnResume);
    addObject(btnPause, /** wherever */);
    // anything else that needs done when resuming a game gets coded here
}

private void pauseGame() {
    removeObject(btnPause);
    addObject(btnExit, /** wherever */);
    addObject(btnResume, /** wherever */);
    // anything else that needs done when pausing the game gets coded here
}
You need to login to post a reply.