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.

