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

2019/11/15

AUTUM PROJEXT - Greenfoot book scenario

dROS dROS

2019/11/15

#
I am so confused . can anybody help me need to supply code in method checkMouseClick(), so that whenever you click a single leaf, this single leaf will change color(change image), and all other leaves does not change colors (images).
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * A block that bounces back and forth across the screen.
 * 
 * @author Michael Kölling
 * @version 1.0
 */
public class Block extends Actor
{
    private int delta = 2;

    /**
     * Move across the screen, bounce off edges. Turn leaves, if we touch any.
     */
    public void act() 
    {
        move();
        checkEdge();
        checkLeaf();
        checkMouseClick();
    }

    /**
     * Move sideways, either left or right.
     */
    private void move()
    {
        setLocation(getX()+delta, getY());
    }

    /**
     * Check whether we are at the edge of the screen. If we are, turn around.
     */
    private void checkEdge()
    {
        if (isAtEdge()) 
        {
            delta = -delta;
        }
    }

    /**
     * Check whether the mouse button was clicked. If it was, change all leaves.
     */
    private void checkMouseClick()
    {
        if (Greenfoot.mouseClicked(null)) 

        {
            World world = getWorld();
            List<Leaf> leaves = world.getObjects(Leaf.class);

            for (Leaf leaf : leaves)
            {
                leaf.changeImage();
            }
        }
    }

    /**
     * Check whether we're touching a leaf. If we are, turn it a bit.
     */
    private void checkLeaf()
    {
        Leaf leaf = (Leaf) getOneIntersectingObject(Leaf.class);

        if (leaf != null) {
            leaf.turn(9);
        }
    }
}
import greenfoot.*;

/**
 * A floating leaf that blows across the screen.
 *
 * @author Michael Kölling
 * @version 1.0
 */
public class Leaf extends Actor
{
    private int speed;
    GreenfootImage img1 = new GreenfootImage("leaf-green.png");
    GreenfootImage img2 = new GreenfootImage("leaf-brown.png");
    
    /**
     * Create the leaf.
     */
    public Leaf()
    {
        setImage(img1);
        speed = Greenfoot.getRandomNumber(3) + 1;      // random speed: 1 to 3
        setRotation(Greenfoot.getRandomNumber(360));
    }
    
    /**
     * Move around.
     */
    public void act() 
    {
        if (isAtEdge()) 
        {
            turn(180);
        }
        
        move(speed);
        
        if (Greenfoot.getRandomNumber(100) < 50) 
        {
            turn(Greenfoot.getRandomNumber(5) - 2);   // -2 to 2
        }
    }
    
    /**
     * Change the image to another leaf image. This toggles back and
     * forth between two images.
     */
    public void changeImage()
    {
        if (getImage() == img1) 
        {
            setImage(img2);
        }
        else {
            setImage(img1);
        }
    }
}
import greenfoot.*;

/**
 * Autumn. A world with some leaves.
 * 
 * @author Michael Kölling
 * @version 1.0
 */
public class MyWorld extends World
{
    /**
     * Create the world and objects in it.
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        setUp();
    }
    
    /**
     * Create the initial objects in the world.
     */
    private void setUp()
    {
        int i = 0;
        while (i<18) {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject( new Leaf(), x, y );
            i++;
        }
        
        addObject(new Block(), 300, 200);
    }
}

danpost danpost

2019/11/15

#
You will need to get a MouseInfo object when a click is detected and use the getActor method on it. If the returned reference is not null and if it is an instance of the Leaf class, you can call the changeImage method on the actor when cast as a Leaf object.
dROS dROS

2019/11/19

#
I still didn't get it . I am new to programming. can you see what i did wrong ? please
 if (Greenfoot.mouseClicked(getActor()))
        {
             Leaf actor = getActor();
           List<Leaf> leaves = actor.getObjects(Leaf.class);
           for (Leaf leaf : leaves)
        {
        leaf.changeImage();
        }
        }
Super_Hippo Super_Hippo

2019/11/20

#
Any reason why it has to be checked in the Block class code instead of the Leaf class code? In the Leaf class, it can simply be:
if (Greenfoot.mouseClicked(this)) changeImage();
To do what danpost said, it will look like this:
if (Greenfoot.mouseClicked(null))
{
    Actor actor = Greenfoot.getMouseInfo().getActor();
    if (actor != null && actor instanceof Leaf)
    {
        ((Leaf) actor).changeImage();
    }
}
You need to login to post a reply.