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

2016/11/4

Trying to use a list to affect pears when my block hits a world edge

JWK3986 JWK3986

2016/11/4

#
So far everything works except resetting the location of the pear when it hits the right edge of the screen. each pear moves 20 cells right when the Block class bounces off a wold edge. The pear should move from the right edge on contact to the same height on the left edge. the movePears() method should be able to do this but nothing I've tried works correctly.
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;
    double dx;
    double dy;
    /**
     * Move across the screen, bounce off edges. Turn leaves, if we touch any.
     */
    public void act() 
    {
        move();
        checkEdge();
        checkLeaf();
        checkMouseClick();
        turnApples();
        movePears();
    }

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

    /**
     * Method to turn apples 90 degrees when Block hits an edge
     */
    public void turnApples()
    {
        if (isAtEdge() == true)
        {
            World world = getWorld();
            List<Apple> apples = world.getObjects(Apple.class);

            for (Apple apple : apples)
            {
                apple.turn(90);
            }

        }

    }

    /**
     * Method to move Pears 20 cells to the right and 
     * move them to the opposite edge if at the world edge
     */ 
    public void movePears()
    {

        if (isAtEdge() == true)
        {
            World world = getWorld();
            List<Pear> pears = world.getObjects(Pear.class);
            //if (isAtEdge() == false)
            for (Pear pear : pears)
            {

                {

                    pear.move(20);

                    
                       
                        
                    if (getX() == getWorld().getWidth()) {
                        pear.setLocation(0, getY ());
                         


                    }

                }
            }
        } 

    }

    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }

}
danpost danpost

2016/11/4

#
Try changing line 118 to:
if (getX() == getWorld().getWidth()-1) {
The first column of pixel is at an x-coordinate of zero; so, however wide your world is, the last column is at one less than the world width.
JWK3986 JWK3986

2016/11/4

#
That made the pear spawn at the left edge, but not at the same height. After that it doesn't behave as it should either, it only moves to the right once then respawns at the left edge in the middle. I was able to get a result similar to this last night, but the pears continued to move to the right all spawning right in the middle of the left world edge. For my lab I need to find a way to get the block code to contain the method to move the pears right and spawn them on the left edge if they hit the right edge, but I've only been able to get it to work by putting code to move an actor to the opposite side in a bounded world that you put in a reply to a post in the Block code.
JWK3986 JWK3986

2016/11/4

#
Using the information you supplied in This Post but my professor said it should be done in the Block code, not the Pear code and above I meant I got it to work by adding the code you supplied here to the Pear source code, bnot Block, I said that wrong sorry
danpost danpost

2016/11/4

#
Ahh -- in lines 118 and 119, you are getting coordinates from the block instead of from the pear. preceed all 'getX' and 'getY' in those lines with 'pear.'.
JWK3986 JWK3986

2016/11/4

#
I may have made some progress. I tried using getOneIntersectingObject in the Block code in order to call methods in the pear class from the Block code, if I'm understanding this correctly. When it returns a null since I'm not looking for pears intersecting the block, but the world edge, I tried invoking (pear.isAtEdge() == true) to then swap any Pear at the edge to the setLocation value, but it does not seem to be behaving quite as intended. The pears spawn on the left edge after hitting the right edge and all continue moving to the right as intended, but they are all in the exact center of the Y coords.
  /**
     * Method to move Pears 20 cells to the right and 
     * move them to the opposite edge if at the world edge
     */ 
    public void movePears()
    {

        if (isAtEdge() == true)
        {
            World world = getWorld();
            List<Pear> pears = world.getObjects(Pear.class);
            //if (isAtEdge() == false)
            for (Pear pear : pears)
            {

                {

                    pear.move(20);

                    
                       
                    getOneIntersectingObject(Pear.class);
                    if (pear != null)
                    {
                      if(pear.isAtEdge()== true)
                      {
                        pear.setLocation(0, getY()); 
                        
                    }
                    
                         


                    }

                }
            }
        } 

    }
JWK3986 JWK3986

2016/11/4

#
ok let me give that a shot
JWK3986 JWK3986

2016/11/4

#
ok, everything executed properly when I added "pear." in the appropriate spots. Thanks a lot for the help.
  /**
     * Method to move Pears 20 cells to the right and 
     * move them to the opposite edge if at the world edge
     */ 
    public void movePears()
    {

        if (isAtEdge() == true)
        {
            World world = getWorld();
            List<Pear> pears = world.getObjects(Pear.class);
            //if (isAtEdge() == false)
            for (Pear pear : pears)
            {

                {

                    pear.move(20);

                    if (pear.getX() == getWorld().getWidth()-1) {
                        pear.setLocation(0, pear.getY());

                    }
                    //getOneIntersectingObject(Pear.class);
                    //if (pear != null)
                    //if(pear.isAtEdge()== true)
                    // pear.setLocation(0, getY()); 

                    

                }
            }
        }
    } 

}
You need to login to post a reply.