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

2017/3/25

Buttons cannot be clicked multiple times

Telko Telko

2017/3/25

#
Good day everyone At the beginning of my game I have a startsceen where the user can select a gamemode through buttons and a small description pops up. When one Button (all are generated in one class) is clicked the others dont work anymore. This is my Button class:
    private int funktion;
    Description description = new Description();
    /**
     * Ermöglicht mehrere Buttons mit unterschiedlichen Funktionen in einer Klasse.
     */
    public Button(int width, int height, String pName, int pFunktion) {
        GreenfootImage image = new GreenfootImage("Button.png");
        image.drawString(pName, width/2-pName.length()*3, height/2);
        funktion = pFunktion;
        setImage(image);
    }
    
    /**
     * Überprüft ob ein Button gedrückt wurde.
     */
    public void act() 
    {
        if (funktion==1 && Greenfoot.mouseClicked(this)) {
                Welt.gamemode = 1;
                getWorld().addObject(description, 242, 371) ;
                
            } 
        if ( funktion==2 && Greenfoot.mouseClicked(this)) {
                Welt.gamemode = 2;
                getWorld().addObject(description, 242, 371) ;
                
            }
        if (funktion == 3 && Greenfoot.mouseClicked(this)) {
                Welt.gamemode = 3;
                getWorld().addObject(description, 242,371) ;
            }
It behaves like: when one codition is executed once none can be used again. My goal is to make it so the user can switch between the gamemodes (and the desciptions) till he finds one he wants to play
Super_Hippo Super_Hippo

2017/3/25

#
Maybe you have to remove the description before adding a new one. So right now, you might not see the other description. You could click two buttons, then pause the scenario and move the description away. Do you see another one?
danpost danpost

2017/3/25

#
To avoid multiple descriptions, one over another, you can add the following to the Description class:
protected void addedToWorld(World world)
{
    removeTouching(Description.class);
}
Also, you can simplify the act method in the Button class to the following (although it probably will not change its current behavior):
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        if (funktion >= 1 && funktion <= 3)
        {
            Welt.gamemode = funktion;
            getWorld().addObject(description, 242, 371);
        }
You need to login to post a reply.