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

2016/11/13

How can I get the images on the left side of the screen to change with a mouse click?

Astralman Astralman

2016/11/13

#
I need to get the leaves on the left side of the image to change, not all the images. From another actor class:
private void checkMouseClick()
    {
        if (Greenfoot.mouseClicked(null)) 
        {
            World world = getWorld();
            List<Leaf> leaves = world.getObjects(Leaf.class);

            for (Leaf leaf : leaves)
            {
                leaf.changeImage();
            }
        }
    }
From Leaf actor class:
public class Leaf extends Actor
{
    GreenfootImage img1 = new GreenfootImage("leaf-green.png");
    GreenfootImage img2 = new GreenfootImage("leaf-brown.png");
    

    public Leaf()
    {
        setImage(img1);
}

public void changeImage()
    {
        if (getImage() == img1) 
        {
            setImage(img2);
        }
        else {
            setImage(img1);
        }
    }
}
Super_Hippo Super_Hippo

2016/11/13

#
Line 10 needs a condition:
if (leaf.getX() < world.getWidth()/2+1)
{
    leaf.changeImage();
}
You need to login to post a reply.