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

2016/12/8

Autumn Apples turning

DocWhiskey DocWhiskey

2016/12/8

#
Hey guys I'm back again since this website has been so helpful in the past. So I am working on this Drill and Practice thing for Chapter 7 and I am pretty much done for the most part EXCEPT for one small detail. Here i will post the code
import greenfoot.*;
import java.util.List;
 
/**
 * A block that bounces back and forth across the screen.
 * 
 * @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.
     */
    public 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();
            }
            
        }
    }
 
    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);
        }
    }
}
So what I am trying to do is have the apples in the world turn 90 degrees every time the block hits the side of the world. Here, i have it so the block turns the apple everytime it touches which is what i thought was supposed to happen but i guess not. So any ideas as to what i need to change?
danpost danpost

2016/12/8

#
So, you won't be needing any intersecting apple (remove line 78) and the condition is changed from having an intersecting apple to being at an edge of the world (line 80 needs adjusted to use 'isAtEdge()'). Then, since you will be turning ALL apples and not just one, line 81 will incorporate a loop to turn every apple that is currently in the world.
DocWhiskey DocWhiskey

2016/12/15

#
danpost wrote...
So, you won't be needing any intersecting apple (remove line 78) and the condition is changed from having an intersecting apple to being at an edge of the world (line 80 needs adjusted to use 'isAtEdge()'). Then, since you will be turning ALL apples and not just one, line 81 will incorporate a loop to turn every apple that is currently in the world.
I still do not fully understand, can you explain it to me? I am taking out the intersectingobject but after that I have no clue what to do. I keep trying to do isAtEdge but it keeps generating errors and its frustrating
Nosson1459 Nosson1459

2016/12/16

#
If you want it to turn all apples when the block isAtEdge then you will have to do a for each loop for all apples in the world to turn(90) if(isAtEdge()) (Your supposed to figure out the (rest of the) coding).
danpost danpost

2016/12/16

#
DocWhiskey wrote...
I still do not fully understand, can you explain it to me? I am taking out the intersectingobject but after that I have no clue what to do. I keep trying to do isAtEdge but it keeps generating errors and its frustrating
You do not need to check for apples at all. You can remove the entire 'checkApple' method. You already have a 'checkEdge' method with an 'isAtEdge' call in it. You just need a bit more code in it to turn the apples there (in the 'if' block code). Use the same code format as the code that changes the images of all the leaves.
You need to login to post a reply.