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

2021/6/29

Making a button visible on my main screen when run is started

xixEmilyxix xixEmilyxix

2021/6/29

#
I'm making a main menu and I would like make it so there is a start button that when you hit it changes to the main screen. I've created the button as an actor and made it so I can change the screen but when I hit run it does not appear, I have to right click the class on the side and pick new StartButton() and then place it. To summarise, I want the button to be on the screen when run is pressed. Code about button:
public void act() 
    {
        Actor StartButtton = new StartButton();
        setImage(new GreenfootImage("StartButton.png")); 
        if(Greenfoot.mouseClicked(this)){
            Greenfoot.setWorld(new BeeWorld());
        }
}
danpost danpost

2021/6/30

#
xixEmilyxix wrote...
I'm making a main menu and I would like make it so there is a start button that when you hit it changes to the main screen. I've created the button as an actor and made it so I can change the screen but when I hit run it does not appear, I have to right click the class on the side and pick new StartButton() and then place it. To summarise, I want the button to be on the screen when run is pressed. Code about button: << Code Omitted >>
First ... about the given code. (1) the act method executes repeatedly while the scenario is running; so, you are creating buttons continuously (not what you want to do) (2) as well as line 3, line 4 is repeating, which will continuously set the same image content to "this" actor (not to the newly created button from line 3, which, by the way, is never in any world to be clicked on) Need to provide menu world codes. Plus, need to know if you want button to APPEAR when run is pressed or if it can be present beforehand. You could also include full class of button for fixing.
xixEmilyxix xixEmilyxix

2021/7/2

#
I managed to figure it out today, thanks though. Turns out I did not add the add object part into the world code
You need to login to post a reply.