Iam trying to random spawn 4 barrels from the right, wich works, and remove them when they hit the left edge of the world. But they only respawn when they hit another barrel. heres my code. Could someone help me with finding my fault?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class barrel here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class barrel extends Actor
{
/* Act - do whatever the barrel wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.*/
public void act()
{
spawn();
if(isgeraakt())
{
{
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getWidth());
getWorld().removeObject(this);
}
}
else
{
int x = Greenfoot.getRandomNumber(2);
move(-x);
}
}
public boolean isgeraakt()
{
boolean raak = false;
Actor r = getOneObjectAtOffset(0,0,boot.class);
if ( r !=null)
{
raak = true;
}
return raak;
}
public void spawn()
{
List objectslookingfor = getWorld().getObjects(barrel.class);
if(objectslookingfor.size() <= 3)
{
getWorld().addObject(new barrel(), 760, Greenfoot.getRandomNumber(760) + 450);
}
Actor barrel;
barrel = this.getOneObjectAtOffset(1, 1, barrel.class);
if(barrel != null)
{
getWorld().removeObject(barrel);
return;
}
}
public void ifAtWorldEdge()
{
if (atWorldEdge())
{
getWorld().removeObject(this);
}
}
public boolean atWorldEdge()
{
return getX() == 0 ||
getX() == getWorld().getWidth() - 1 ||
getY() == 0 ||
getY() == getWorld().getHeight() - 1;
}
}
