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

2017/2/6

How can I create a button

Fifilein Fifilein

2017/2/6

#
Hi! I want to create a "Try again" button in my game which appears at the same time with my Game Over Screen. If you click on the button , the game will be restart. Can anyone help me? I don't know how it could work. Thank you in advance, Fifilein
Super_Hippo Super_Hippo

2017/2/6

#
Create an actor. Give it the image with the Try again text on it. When it is clicked, restart the game. Do it step by step. With which step do you need help? What have you tried?
danpost danpost

2017/2/6

#
You could also just skip the actor and use the Game Over Screen background image as one big button. Just draw some text (or a text image) onto the background indicating that a click will restart the game and add the code for it.
Fifilein Fifilein

2017/2/6

#
Thanks you both, what is the method for the mouse which clicks something?
Super_Hippo Super_Hippo

2017/2/6

#
https://www.greenfoot.org/files/javadoc/ Click that link and then choose the "Greenfoot" class. There are a few methods relating the mouse. The method you want to use is probably 'Greenfoot.mousePressed'.
Fifilein Fifilein

2017/2/6

#
Thank you!
Fifilein Fifilein

2017/2/7

#
It doesn't works if the mouse clicked the button Here is my code: Actor Frosch = getOneIntersectingObject (Frosch.class); if (Frosch != null) { World myWorld = getWorld(); GameOver gameover = new GameOver(); myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); TryAgain tryagain = new TryAgain(); myWorld.addObject(tryagain, 300, 300); Actor TryAgain= getOneIntersectingObject (TryAgain.class); if (mouseClicked) { if( mouseClicked!=true) { Greenfoot.setWorld(new frogfly()); } } myWorld.removeObject(this); Greenfoot.stop(); }
Super_Hippo Super_Hippo

2017/2/7

#
Remove the check for the mouse click there. Just add the object. Place this in the TryAgain class:
1
2
3
4
5
6
7
public void act()
{
    if (Greenfoot.mousePressed(this))
    {
        Greenfoot.setWorld(new frogfly());
    }
}
Fifilein Fifilein

2017/2/7

#
Is there a method for restart the running game?
danpost danpost

2017/2/7

#
Fifilein wrote...
Is there a method for restart the running game?
If you mean for a scenario that is in a paused state (the 'Pause' button in the greenfoot frame was pressed or a Greenfoot.stop() command was executed), then no. When a scenario is paused in this way, no code within it is executed except what may be done manually.. I mean, you could add a 'public void start()' method to execute a Greenfoot.start() command in your world subclass so that you can manually start it by executing that method; but, with a 'Run' button so easily accessible, what is the point?! You can keep the scenario running and "pause" the action within your project in other ways, however. Is that what you are wanting to do? If so, do you want the game world to continue to display while paused or some other world that states that your game world is paused?
Fifilein Fifilein

2017/2/7

#
Originally i wanted to reset it, but now it works another way. Do you know a method to remove all objects of one actor class?
danpost danpost

2017/2/7

#
Fifilein wrote...
Originally i wanted to reset it, but now it works another way. Do you know a method to remove all objects of one actor class?
Please note the first two methods here.
Fifilein Fifilein

2017/2/7

#
Thanks you Super_Hippo danpost danpost the methods from the button?
danpost danpost

2017/2/7

#
Fifilein wrote...
danpost the methods from the button?
Please elaborate. I am not sure what you are asking here. Or, maybe you did not realize that 'here' in my last post is linked to a page.
You need to login to post a reply.