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

2015/11/29

Greenfoot.mouseClicked()

Newbie Newbie

2015/11/29

#
Hey guys, I posted something related to this before and this discussion is about the same application, but I unfortunately cannot find my previous post anymore :-( Anyways I fixed the problem I was asking help for, but I have to write an essay about my programme and I am wondering why my first code did compile, but the image of profile.class wouldn't change. After changing one word in the code (changing Greenfoot.mouseClicked(Profile.class) to Greenfoot.mouseClicked(this) it surprisingly worked! Do you know, why it makes such a huge difference, whether I set a class or the given object as a parameter for the method? Here are the two codes: 1) the one which didn't work
public class Profile extends Actor
{
    private GreenfootImage tickbox;     
    private GreenfootImage tickboxtick;
    public Profile()
    {
        tickbox = new GreenfootImage("Tickbox.png");
        tickboxtick = new GreenfootImage("Tickboxtick.png");
        setImage(tickbox);
    }
    public void act() 
    {
        changeImage();
    
    } 
    public void changeImage()
    {
        if(Greenfoot.mouseClicked(Profile.class)) 
        {
            if(getImage()==(tickbox))
            {
                setImage(tickboxtick);
            }
            else
            {
                setImage(tickbox);
            }
        }
    }
}


2) my final code
public class Profile extends Actor
{
    private GreenfootImage tickbox;     
    private GreenfootImage tickboxtick;
    public Profile()
    {
        tickbox = new GreenfootImage("Tickbox.png");
        tickboxtick = new GreenfootImage("Tickboxtick.png");
        setImage(tickbox);
    }
    public void act() 
    {
        changeImage();
    
    } 
    public void changeImage()
    {
        if(Greenfoot.mouseClicked(this)) 
        {
            if(getImage()==(tickbox))
            {
                setImage(tickboxtick);
            }
            else
            {
                setImage(tickbox);
            }
        }
    }
}
thank you in advance for your help!
danpost danpost

2015/11/29

#
There is only one 'mouseClicked' method of the Greenfoot class and its parameter type is declared to be of type Object. The objects you can test for clicks on are an Actor object, the World object or undefined ('null'). The implementation of the method does not allow for detection of clicks on any other object -- including a Class object The keyword, 'this', when used in a member of a subclass of Actor, will refer to the Actor object that the member (the code that calls the 'mouseClicked' method) is being executed for (greenfoot will call the 'act' method on each Actor object in the world while the scenario is running). If used in a member of a subclass of World, 'this' will refer to the World object (the click is check for being in the world AND not on an Actor object). Please refer to the Greenfoot class API documentation and locate the 'mouseClicked' method in it for reference. To clarify further, a class is an object -- but it is not a clickable object; however, an object created from a World or Actor class can be.
Newbie Newbie

2015/11/30

#
Hello danpost, thank you for your comment, I had suspected that already - but how does the class documentation tell me that I cannot use a Class as a parameter?
danpost danpost

2015/11/30

#
This is what it says in the 'Method Details' section on the 'mouseClicked' method:
public static boolean mouseClicked(java.lang.Object obj) < Main Description Omitted > Parameters: obj - Typically one of Actor, World or null
Actually, being the parameter is typed as Object, anything can be passed to the method (a Color object, a String object, any object). However, when a non-null value (any object) is used for the parameter value, only those of Actor or World will ever return a 'true' value due to the implementation of the method itself.
You need to login to post a reply.