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

2017/2/13

Change the image of an actor by clicking

ezuxr ezuxr

2017/2/13

#
if (Greenfoot.mouseClicked(this)) { this.getImage().equals(new GreenfootImage("smiley1.png")); } I have this in one of my actor that I'm trying to change the image by a mouse click... But it didn't work. is there a way to fix it???
Super_Hippo Super_Hippo

2017/2/13

#
Try to use the 'setImage' method. You are comparing the current image to the smiley1 image, but you are not changing the image anywhere.
danpost danpost

2017/2/13

#
A comparison is being made between the current image of the actor and the "smiley1.png" image; but, the result of that comparison is hanging. As written, your code is equivalent to:
if (Greenfoot.mouseClicked(this)) { true; }
// or
if (Greenfoot.mouseClicked(this)) { false; }
while the commands 'true; and 'false;' do nothing. You probably want the following structure:
if (Greenfoot.mouseClicked(this))
{
    if (new GreenfootImage("smiley1.png").equals(getImage())
    {
        setImage("smiley2.png"); // adjust name of file as needed
    }
}
I switched the order of comparison because it is possible for 'getImage' to return a 'null' value; but not so for the newly created image. Your actor may always have an image and will never have it return 'null', but you never know what changes you might do in the future. If this is a one way, permanent change (the new image will be the image of the actor for the remainder of the life of that acto), then the inner 'if' comparison can just be removed altogether.
ezuxr ezuxr

2017/2/13

#
@danpost It doesn't work as I thought... Does this part
if (new GreenfootImage("smiley1.png").equals(getImage()))
mean the actually image of the actor at first and
 setImage("smiley2.png");
this part mean the image that will be changed after it is clicked? For know I followed your structure and there was no difference made after clicking the actor...
danpost danpost

2017/2/13

#
ezuxr wrote...
@danpost It doesn't work as I thought
Show the entire class code where these lines are located.
ezuxr ezuxr

2017/2/14

#
@danpost :
public class Lipsmile extends Face
{
    /**
     * Act - do whatever the Lipsmile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if (Greenfoot.mouseClicked(this)){
            if (new GreenfootImage("smiley1.png").equals(getImage())){
                setImage("smiley2.png");
            }
        }
    }   
}
do I need to import greenfoot Image on the very top? what should I add more?
danpost danpost

2017/2/14

#
ezuxr wrote...
do I need to import greenfoot Image on the very top? what should I add more?
Well, you should have this line at the top:
import greenfoot.*;
This will import the Actor, Greenfoot and GreenfootImage classes (as well as the others in the greenfoot package). I get the feeling that the image of the actor is not a 'smiley1' image and therefore the image will not change. Insert the following at line 14 for testing purposes:
else {
    setImage("smiley1.png");
}
ezuxr ezuxr

2017/2/14

#
@danpost: It works really well! thank you for helping!
You need to login to post a reply.