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

2014/2/23

Make a button do something

Trxnce Trxnce

2014/2/23

#
Hi guys i have just made a menu for my game that has a button that changes image when the mouse is hovered over it, however the button as yet does not do anything because i do not know how so please can someone help, thanks. Here is the code in my button class
import greenfoot.*;
import java.util.List;

public class StartButton extends buttons
{
   
public void act()   
    {  
        MouseInfo mouse = Greenfoot.getMouseInfo();  
        if (mouse != null) {  
            setImage("Button1.png");  
            List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), menu.class);  
            for (Object object : objects)  
            {  
                if (object == this)  
                {  
                    setImage("button2.png"); 
                   
                }  
            }  
        }  
    }}
Trxnce Trxnce

2014/2/23

#
i want it so when the button is clicked it starts the game, the game class is MarioLand
Trxnce Trxnce

2014/2/23

#
ok i have made it so it runs the game but the button texture doesnt change? here is the updated code
import greenfoot.*;  
import java.util.List;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
public class StartButton extends Actor  
{  
    public void act()  
    {  
        MouseInfo mouse = Greenfoot.getMouseInfo();     
        if (mouse != null) {    
            setImage("Button1.png");    
            List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), menu.class);    
            for (Object object : objects)    
            {    
                if (object == this)    
                {    
                    setImage("Button2.png");    
                }    
  
            }    
        }   
        if (Greenfoot.mouseClicked(this))    
        {    
            Greenfoot.setWorld(new Marioland());  
        }    
    }     
}  
Trxnce Trxnce

2014/2/23

#
ok guys this is sorted can an admin please close this topic, thanks
danpost danpost

2014/2/23

#
The admin prefers you provide your solution (instead of just saying that 'this is sorted') so others can learn from your experience.
You need to login to post a reply.