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

2015/11/16

Need Help with Greenfoot.mouseClicked();

Newbie Newbie

2015/11/16

#
I want my Object to change its Image when clicked on it. The name of the class is Profile, and the two images are "Tickbox.png" and "Tickboxtick.png". Greenfoot compiles the Code I got so far, but when I click on the Object, it doesn't change it's image. I got this Code so far:
 private GreenfootImage tickbox;     
    private GreenfootImage tickboxtick;
    public Profile()
    {
        tickbox = new GreenfootImage("Tickbox.png");
        tickboxtick = new GreenfootImage("Tickboxtick.png");
    }
    public void act() 
    {
        changeImage();
    
    }  
    public void changeImage()
    {   MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(Profile.class)&& mouse.getButton()==1 )
        {
            if(getImage()==(tickbox))
            {
                setImage("Tickboxtick.png");
            }
            else
            {
                setImage("Tickbox.png");
            }
        }
    }
I'd be really grateful for you to help me with that problem as it's a programme I have to finish as quickly as possible.
danpost danpost

2015/11/16

#
The problem is that the image is never set to 'tickbox' (or 'tickboxtick'); so, line 17 will always be false and the image is set to a different instance of a GreenfootImage object created from the image saved in the "Tickbox.png" file. Make the following changes:
// change line 19 to
setImage(tickboxtick);
// change line 23 to
setImage(tickbox);
// insert at line 7
setImage(tickbox);
Newbie Newbie

2015/11/16

#
Thank you for your answer, I was pretty convinced by that ;) But unfortunately it is still not working... :( I can embed the edited code for you to check, if I did anything other than what you suggested me to do:
    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()
    {   MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(Profile.class)&& mouse.getButton()==1 )
        {
            if(getImage()==(tickbox))
            {
                setImage(tickboxtick);
            }
            else
            {
                setImage(tickbox);
            }
        }
    }
}

danpost danpost

2015/11/16

#
The code given looks okay -- other than a slight improvement that could be made: change 'Profile.class' in line 16 to 'this'. This will probably not solve your issue. If it does not, pause the scenario and right click on the object you were trying to change the image and select 'Inspect' from the pop-up menu to see if it is actually a Profile object. Post back all results of these tests.
You need to login to post a reply.