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

2023/9/9

how do i create a button that does one thing when clicked once then another when clicked again

Owie2712! Owie2712!

2023/9/9

#
i have no code for this yet as im not sure where to start but i want to code a button so when its clicked once it opens another actor but then when it is clicked again and that actor is still present the actor is removed (like a open close button for a hidden menu)
Owie2712! Owie2712!

2023/9/9

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class companyList here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class companyList extends Actor
{
    public boolean Open; 
    public void act()
    {
        Open = false; 
        ownList ownList = new ownList();
        companyname companyname = new companyname();
        if (Greenfoot.mouseClicked(this))
        {
            if(Greenfoot.mouseClicked(this) && (!Open)){
                getWorld().addObject(ownList, 300,430);
                getWorld().addObject(companyname,130,215);
                Open = true;
            }
             if(Greenfoot.mouseClicked(this) && (Open)){
                getWorld().removeObject(ownList);
                getWorld().removeObject(companyname);
                Open = false;
            }
        }
    }
}
this is the code i have now and it opens the thing that i want it to open it just doesnt close it again
Spock47 Spock47

2023/9/9

#
Your implementation idea is correct. There are just some minor mixups: 1. Currently, line 14 sets the boolean "Open" to false in every act. Instead it should only initialize it (set the value ->once<-) with value false: change line 11 to "private boolean Open = false;" and remove line 14 ("open = false;") . 2. lines 15 and 16 create new objects for ownList and companyname in every act. Again, these need to be defined only once - otherwise the object removed in line 25 will not be the same object that was added in a previous run of act in line 20 (rendering the remove statement effectless): Move lines 15 and 16 to line 11 (outside of the method) and prefix them with "private" each, e.g. "private ownList ownList = new ownList();". Most likely, this will already do the trick. Please let us know whether it works or if still something is going wrong. One additional cleanup suggestion: 3. line 17 already checks for the mouse click, so you don't have to check it again in lines 19 and 24, simplify e.g. line 24 to "if (Open) {". Live long and prosper, Spock47
danpost danpost

2023/9/10

#
Spock47 wrote...
Your implementation idea is correct. There are just some minor mixups: << List Omitted >>
Notes about the list: (1) The default value for boolean fields is false, so it is not required to add "= false" to line 11; (2) This may not be necessary if the objects created are equivalent each time. This will become apparent after moving on to (3) below; (3) The state of being "open" can be determined by a simple check, without the use of a boolean field. Note the code below:
import greenfoot.*;

public class companyList extends Actor
{
    private ownList ownList = new ownList();
    private companyname companyname = new companyname();
    
    public void act() {
        if (Greenfoot.mouseClicked(this)) {
            if (ownList.getWorld() == null) {
                getWorld().addObject(ownList, 300, 430);
                getWorld().addObject(companyname, 130, 215);
            }
            else {
                getWorld().removeObject(ownList);
                getWorld().removeObject(companyname);
            }
        }
    }
}
The alternative (if creating the object each time they are added (provided only one ownList object and one companyname object are in the world at any time):
import greenfoot.*;

public class companyList extends Actor
{
    pubic void act() {
        if (Greenfoot.mouseClicked(this)) {
            if (getWorld().getObjects(ownList.class).isEmpty()) {
                getWorld().addObject(new ownList(), 300, 430);
                getWorld().addObject(new companyname(), 130, 215);
            }
            else {
                getWorld().removeObjects(getWorld().getObjects(ownList.class));
                getWorld().removeObjects(getWorld().getObjects(companyname.class));
            }
        }
    }
}
You need to login to post a reply.