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

2015/11/23

Can't get a class to run a method in the World.

mickyg mickyg

2015/11/23

#
I am having an issue where I can't get the mouse click in my StartButton class act( ) method to run my prepare( ) method in the Backdrop( ) world class. Not sure why this won't work.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Backdrop here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Backdrop extends World
{
    
    /**
     * Constructor for objects of class Backdrop.
     * 
     */
    public Backdrop()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        menu();
    }

    public void menu()
    {
        removeObjects(getObjects(Actor.class));
        setBackground("atari.jpg");

        StartButton start = new StartButton();
        addObject(start, getWidth()-100,getHeight()/2);

        Mouse m = new Mouse();
        addObject(m, 300, 200); 

    }

    public void prepare()
    {
        removeObjects(getObjects(Actor.class));
        setBackground("granitelight.jpg");
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class StartButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StartButton extends Actor
{
    /**
     * Act - do whatever the StartButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Backdrop backdrop = (Backdrop)getWorld();
        MouseInfo mouse = Greenfoot.getMouseInfo();
        
         if(mouse!=null)
        {
            if(Greenfoot.mouseClicked(this))
            {   
                backdrop.prepare();
            }
            
            if(mouse.getX()>475&&mouse.getX()<525&&mouse.getY()>166&&mouse.getY()<234)
            {
                setImage("starthover.png");
            }
            else
            {
                setImage("start.png");
            }

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

/**
 * Write a description of class Mouse here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mouse extends Actor
{
    public Mouse()
    {
        //setImage("cursor.png");        
    }
    
    /**
     * Act - do whatever the Mouse wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse!=null)
        {
            setLocation(mouse.getX(), mouse.getY());
        }
        
        if(mouse==null)
        {
            GreenfootImage image = new GreenfootImage(20, 20);
            getImage().setTransparency(0);
        }
        else if(mouse!=null)
        {
            GreenfootImage image = new GreenfootImage(20, 20);
            getImage().setTransparency(255);
            setLocation(mouse.getX(), mouse.getY());
        }
    }
    
    public void setTransparency(int t){       
    }
}
danpost danpost

2015/11/23

#
You are probably clicking on the Mouse object instead of the StartButton object (the object which follows the mouse is above the start button when you try to click on it).
mickyg mickyg

2015/11/23

#
Thank you again! Is there a way to change the mouse arrow to something else to remedy the situation? Having the Start object above the mouse object works, but it doesn't look right because the mouse object goes behind the start when you hover over it.
danpost danpost

2015/11/23

#
If the Mouse object is to have an image that shows over top of the StartButton object, there is no way to click on the StartButtton object. Therefore, you need to detect a click on the Mouse object and determine if the Mouse object intersects the StartButton object. The other thing you could do is change the actual mouse icon itself and remove the Mouse object class you created.
You need to login to post a reply.