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

2025/6/6

Need help making a button

no_name no_name

2025/6/6

#
i need some help making a button click. I know it is very simple to make a button in any game, but i don't know why it is so complicated because i can't get my button to click properly. Some sources say that my buttons transparent background is messing with the clicking, but im not sure. In the code i provided i made it so when the button is clicked it will do move(4); just so i can check whether my button is clicking or not. it kind of works but it doesn't allow me to click on the image it self and it only lets me click outside of the buttons image where the transparent background is. here is my code:
import greenfoot.*;

public class Play_button extends Actor
{
    
    public Play_button() {
        GreenfootImage pB = new GreenfootImage("play_button.png");
        pB.scale(pB.getWidth()/3, pB.getHeight()/3);
        setImage(pB);  
    }
    
    public void act()
    {
       
       if (Greenfoot.mouseClicked(this))
       {
           move(4);
       }
    }
}
danpost danpost

2025/6/6

#
Should you create one of these Play_Button objects and add it into the would, it should move 4 to the right when clicked, provided it is not under another actor.
no_name no_name

2025/6/7

#
i got it to work, thank you for replying though. What do you mean by if it is not under another actor. Are you talking about the side panel to the right and the ordering of the actors?
danpost danpost

2025/6/8

#
no_name wrote...
i got it to work, thank you for replying though. What do you mean by if it is not under another actor. Are you talking about the side panel to the right and the ordering of the actors?
I was talking in general. If any actor (invisible or not) overlays (on top, in ordering) the actor in question, the overlaying actor will get that click instead.
You need to login to post a reply.