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

2012/10/29

How can I make a button?

1
2
danpost danpost

2012/11/4

#
What do you mean by 'automatically'? please elaborate. When are you adding the button into the world? Will it be in the world from the start, or added later? if later, what trigger is used to signify that you need to add it into the world (what kind of class is the trigger in)? How long will the button remain in the world? The coordinates are set when you add the button object into the world with 'addObject(button, x-coordinate, y-coordinate);' or 'getWorld().addObject(button...);'.
Andronious Andronious

2012/11/6

#
I figured that out thank you. I have one more question however, how do I go to a different World once i clicked on the button? I used setWorld(Game); but it doesnt work. thanks
danpost danpost

2012/11/6

#
If 'Game' is the name of the world class that you want to go to, then use
Greenfoot.setWorld(new Game());
Kappa Kappa

2013/10/26

#
Hi, Gevater_Tod4711 I've updated your code
public class Button extends Actor
{
    private GreenfootImage button_1 = new GreenfootImage("image_button_no_pressed.png");  
    private GreenfootImage button_2 = new GreenfootImage("image_button_pressed.png");  
  
    private boolean mouseDown;  
      
    public Button() {  
        setImage(button_1);  
        mouseDown = false;  
    }  
  
    public void act() {  
        if (!mouseDown && Greenfoot.mousePressed(this)) {    
            setImage(button_2); 
            mouseDown = true; // here
        }    
        if (mouseDown && Greenfoot.mouseClicked(this)) {  
            setImage(button_1);  
            mouseDown = false; // and here
            //add also the methods you want to execute here;  
        }
        
    }   
}
You need to login to post a reply.
1
2