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 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?
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);
}
}
}
