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
pashmi pashmi

2012/10/29

#
Basicly, a game I'm going to make for my class would be better if I added a button to spawn objects for an amount of points. Can someone tell me how to make a button that performs a method? I'm pretty sure:
if (Greenfoot.mouseClicked(this))

{
methods();
}
Would work-however, is there a way I could give the feel that the button is really being pressed? For example, the shade of the object I put this script in would change/get darker and possibly the object could get smaller WHILE being pressed. Can someone help me out here?
pashmi pashmi

2012/10/29

#
code in*
pashmi pashmi

2012/10/29

#
Help please.
Gevater_Tod4711 Gevater_Tod4711

2012/10/29

#
you first need a class button (or however you want to call it). In this class you write the code you already posted in the act method like this:
public class Button exdends Actor {
    public void act() {
        if (Greenfoot.mouseClicked(this)) {//true if you clicked at this object;
            setImage(otherButton); //changes the image of the button so that you see if it's clicked;
            //also add the methods you want to execute;
        }
    }
}
pashmi pashmi

2012/10/29

#
quick question-how would i reference to the other button so it can create it? would i just need 2 classes? like button1 and button2?
Gevater_Tod4711 Gevater_Tod4711

2012/10/29

#
do you mean you need two buttons or do you mean this: setImage(otherButton) ? if you want two buttons you can just add two objects from the type Button. if you mean setImage(otherButton): this is no real button but an image. You can just change the image if this object (like the method already says: setImage)
danpost danpost

2012/10/29

#
For the appearance of the button being pressed, you will of course need two images. Use the mouse function 'mousePressed' and 'mouseClicked' to toggle the image. You will need a boolean variable in the class to track the state of the mouse.
// instance field
boolean mouseDown;
// image control in act method
if (!mouseDown && mousePressed(this))
{
    // switch image
}
if (mouseDown && mouseClicked(this))
{
    // swith image back
    // call method(s)
}
pashmi pashmi

2012/10/30

#
but how do i declare the image?
Gevater_Tod4711 Gevater_Tod4711

2012/10/30

#
try this:
public Button extands Actor {
    
    private GreenfootImage button_1 = new GreenfootImage("the filename of the first image");
    private Greenfootimage button_2 = new GreenfootImage("the filename of the second image");

    private boolean mouseDown;
    
    public Button() {
        setImage(button_1);
        mouseDown = false;
    }

    public void act() {
        if (!mouseDown && mousePressed(this)) {  
            setImage(button_2); 
        }  
        if (mouseDown && mouseClicked(this)) {
            setImage(button_1);
            //add also the methods you want to execute here;
        }  
    }
    
}
pashmi pashmi

2012/11/2

#
What's the difference between mousePressed and mouseClicked? Oh and I think you got the button_2 and button_1 wrong way around. But thanks a lot for your help!
Gevater_Tod4711 Gevater_Tod4711

2012/11/2

#
At the greenfoot API you can read about every method you can use in greenfoot. Could be helpfull. The difference between mousePressed and mouseClicked is that mouse pressed is true if the mouse key is down and mouseClicked is true if the mouse key has ben clicked and released.
Andronious Andronious

2012/11/4

#
It says that it cannot find symbol mousePressed or mouseClicked. I tried changing it to dragged and dragEnded but it says the same thing.
danpost danpost

2012/11/4

#
'mousePressed', 'mouseClicked', 'mouseDragged' and 'mouseDragEnded' are all methods in the Greenfoot class of the Greenfoot API. You must prefix them with 'Greenfoot.' (with the 'dot' at the end). Sorry about that. I guess I missed that myself when I wrote out the code above.
Andronious Andronious

2012/11/4

#
Thank you
Andronious Andronious

2012/11/4

#
Thank you
There are more replies on the next page.
1
2