Please help! I'm attempting to add code in the Block class so that each time the block hits the edge, all pears are moved 20 cells to the right and the apples turn 90 degrees whenever the block hits the edge of the world. I am using move and turn commands, perhaps I should use setLocation??
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();
checkLeaf();
checkMouseClick();
turnApple();
movePear();
}
/**
* 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);
List<Apple> apples = world.getObjects(Apple.class);
List<Pear> pears = world.getObjects(Pear.class);
for (Leaf leaf : leaves)
{
leaf.changeImage();
}
for (Apple apple : apples)
{
//turn(90);
}
//for (Pear pear : pears)
for (Object obj : world.getObjects(Pear.class))
{
//move(20);
}
}
}
/**
* 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);
}
}
private void turnApple()
{
if (isAtEdge() )
{
turn(90);
}
}
private void movePear()
{
Pear pear = (Pear) getOneIntersectingObject(Pear.class);
if (pear != null) {
pear.move(20);
}
}
}
