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

2016/12/7

Changing half of Images

georgette georgette

2016/12/7

#
Hello, I need to modify the method that handles the mouse click so that only images on the left half of the world are changed, right now they all change. So far everything I've tried has not worked. As always, I really appreciate your help!

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.
     * If apple turn. If pear move.
     */
    private void checkEdge()
    {        
        World world = getWorld();        

        if (isAtEdge()) 
        {
            delta = -delta;   
            List<Apple> apples = world.getObjects(Apple.class);
            List<Pear> pears = world.getObjects(Pear.class);
            for (Apple apple : apples) 
            {
                apple.turn(90);
            }
            for (Pear pear :  pears) 
            {
                pear.move(-20);
            }

        }           

    }

    /**
     * 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();               
            }
            setLocation(getX() -6, getY() );
        }

        //if (leaf.getX() < world.getWidth()/2+1)
        //{
          // 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);
        }         

    }

}
danpost danpost

2016/12/7

#
georgette wrote...
Hello, I need to modify the method that handles the mouse click so that only images on the left half of the world are changed, right now they all change.
Replace line 65 with the commented lines below it and uncomment them.
georgette georgette

2016/12/8

#
Thank you!! Now I can see why it wouldn't work on line 70.
You need to login to post a reply.