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

2016/12/2

Object Interaction

georgette georgette

2016/12/2

#
Please help! I'm attempting to add code in the Block class so that each time the block hits the edge, all pears are moved 20 cells to the right and the apples turn 90 degrees whenever the block hits the edge of the world. I am using move and turn commands, perhaps I should use setLocation??

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();
        turnApple();
        movePear();       

    }
    
    /**
     * 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);
            List<Apple> apples = world.getObjects(Apple.class);
            List<Pear> pears = world.getObjects(Pear.class);

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

            {
                //turn(90);
            }
            //for (Pear pear : pears)
            for (Object obj : world.getObjects(Pear.class))
            {
                //move(20);                
            }
        }
    }

    /**
     * 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);
        }
    }

    private void turnApple()
    {

        if (isAtEdge() )
        {
            turn(90);  
        }

    }

    private void movePear()
    {
        Pear pear = (Pear) getOneIntersectingObject(Pear.class);

        if (pear != null) {
            pear.move(20);
        }      

    }

}  
danpost danpost

2016/12/2

#
Using the 'move' command for Pear object is okay only if the pears never rotate (neither by 'turn' nor 'setRotation'); otherwise, you will need to use 'setLocation'. You say you want that these actions happen when the block hits a wall, but I see the codes for those actions in the 'checkMouseClick' method -- strange! I would think that they would be attempted in the 'checkEdge' method.
georgette georgette

2016/12/3

#
Thank you danpost! I'm not sure if I should have started a new thread when following up or if this is ok. I re-arranged the code to checkEdge and it compiles with no syntax errors, however when I run it in Greenfoot, I receive the error message: java.lang.ClassCastException: java.util.ArrayList cannot be cast to Apple; at Block.checkApple(Block.java:62); at Block.act(Block.java:31). I think an array is useful for holding one or more variables, please help!
{
    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();
        List<Apple> apples = world.getObjects(Apple.class);
        List<Pear> pears = world.getObjects(Pear.class);
        Apple apple = (Apple)getIntersectingObjects(Apple.class);
        Pear pear = (Pear)getIntersectingObjects(Pear.class);

        if (isAtEdge()) 
        {
            delta = -delta;            
        }   
        if (apple != null) {
            apple.turn(90);
        }
        if (pear != null) {
            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();
            }

        }
    }

    /**
     * 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/3

#
When a block hits an edge, you want to work with all apples and pears. Lines 34 and 35 gets all intersecting apples and pears; however these List objects are attempted to be cast as Apple and Pear objects. A list is neither an apple or a pear. At any rate, I do not see why you need to acquire the lists of apples and pears that intersect the block when the block is at the edge of the world. Remove lines 34 and 35; with that, you should remove lines 41 through 46 as well. Now, the issue is how to turn all apples and move all pears in the world when the block hit an edge. You already have the lists required via lines 32 and 33; but there is no need to acquire these lists unless the block is at the edge. The code executed when the block is at the edge is currently line 39. This is where you should also acquire the lists. Cut lines 32 and 33 and insert them at line 40. Follow that with the following lines:
for (Apple apple : apples) apple.turn(90);
for (Pear pear  pears) pear.move(20);
These lines iterate through the lists of apples and pears and performs the required actions on each one.
georgette georgette

2016/12/4

#
You are the best danpost.. it works! Thank you!!
You need to login to post a reply.