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

2016/1/8

Turn an Apple at Edge

abc2048 abc2048

2016/1/8

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * A block that bounces back and forth across the screen.
 * 
 * @author Alexander Case
 */
public class Block extends Actor
{
    private int delta = 2;
    
    /*
     * World world = getWorld();
     * world.addObject(new Leaf(), 100, 100);
     * 
     * int x = Greenfoot.getX()
     * int y = Greenfoot.getY()
     */
    
    /**
     * Move across the screen, bounce off edges. Turn leaves, if we touch any.
     */
    public void act() 
    {
        move();
        checkEdge();
        checkLeaf();
        checkMouseClick();
        turnApple();
    }
    
    /**
     * Move to the left and right
     */
    private void move()
    {
        setLocation(getX()+delta, getY());
    }
    
    /**
     * This checks if we are at the edge and to turn around if we are
     */
    private void checkEdge()
    {
        if (isAtEdge()) 
        {
            delta = -delta;
        }
      } 
     
    private void turnApple()
    {
       if (isAtEdge())
      {
        turn(90);
      }
    }

      
    /**
     * This checks if the mouse was clicked and if it was it will change the leaf
     */
    private void checkMouseClick()
    {
        if (Greenfoot.mouseClicked(null)) 
        {
            World world = getWorld();
            List<Leaf> leaves = world.getObjects(Leaf.class);

            for (Leaf leaf : leaves)
            {
                leaf.changeImage();
            }
        }
    }
    
    /**
     * If the block is touching a leaf it will turn a bit
     */
    private void checkLeaf()
    {
        Leaf leaf = (Leaf) getOneIntersectingObject(Leaf.class);
        
        if (leaf != null) {
            leaf.turn(9);
        }
    }
}
abc2048 wrote...
I have posted some code that does not do what I intended. Right now the Block turns 90 degrees. But I want my Apple to turn by 90 degrees when it is at the edge. I don't know how to call the Apple instead of the Block to do this. Right now the Block turns 90 degrees. The end result is that I want all instances of the Apple class to turn when the Block hits the edge.
danpost danpost

2016/1/8

#
To have an actor get a list of all apples in the world, you can use the following:
World world = getWorld();
java.util.List<?> apples = world.getObjects(Apple.class);
Now, I only have shown this to show what is assigned to 'apples', here. The important thing is the expression on the right side of the second line. We do not have to outwardly assign this expression to a List variable to iterate through the list:
for (Object obj : world.getObjects(Apple.class))
This will place each apple (one at a time, while iterating through the list) in the 'obj' variable. From that you can change the rotation of each one:
((Actor)obj).turn(90);
DanielRLara DanielRLara

2016/11/16

#
import greenfoot.*;
import java.util.List;

/**
 * A block that bounces back and forth across the screen.
 * 
 * @author Daniel Lara
 * @version 0.1
 */
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();
        checkMouseClick();
        checkForLeaf();
        checkApple();
    }

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

    /**
     * Check whether the mouse button was clicked. If it was, change all leaves.
     */
    private void checkMouseClick()
    {
        if (Greenfoot.mouseClicked(null)) 
        {
            MyWorld earth = (MyWorld) getWorld();
            List<Leaf> allLeafs = earth.getObjects(Leaf.class);
            //For Each Loop
            for( Leaf jahn : allLeafs)
            {
                jahn.changeImage();
            }
            earth.populate();
        }
    }

    public void checkForLeaf()
    {
        if(isTouching(Leaf.class))
        {
            Leaf leafy = (Leaf) getOneIntersectingObject(Leaf.class);
            leafy.turn(9);
            leafy.changeImage();
            World earth = getWorld();
            leafy.setLocation( Greenfoot.getRandomNumber(earth.getWidth()), Greenfoot.getRandomNumber(earth.getHeight()) );
        }
    }

    private void checkApple()
    {
        Apple apple = (Apple) getOneIntersectingObject(Apple.class);

        if (apple != null) {
            apple.turn(9);
        }
    }
}
Hi, I would like some help with making it so ALL the apples in my world turn 90 degrees every time the block hits the edge of the world. Right now one apple turns 90 degrees but only when the block touches that apple. I would appreciate if you could elaborate on what you wrote earlier, as abc2048 looked to have the same problem as me, but I didn't quite understand. Thanks for anyone's help.
Super_Hippo Super_Hippo

2016/11/16

#
Reread the end of danposts reply. Your question is answered there. If you look at your 'checkMouseClick' method, you will see that you do something to all leafs. To do something with all apples is quite similar.
You need to login to post a reply.