hey guys i´m new here. Can anyone tell me how to sporn an actor on a random place?
thx for anser me
addObject(new YourClassName(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
getWorld().addObject(new YourClassName(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
public void act() { if (getWorld().getObjects(Walter.class).isEmpty()) { getWorld().addObject(new Walter(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight())); } }
private int counter = 0; public void act() { counter++; if (counter > 500) { //this value depends on your scenario speed; I'm not shure which value is about 10 seconds; You'll have to try; getWorld().addObject(new Walter(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight())); counter = 0; } }
//in WORLD class // in 'act' method if (getObjects(Walter.class).isEmpty()) spawnWalter(); // new method private void spawnWalter() { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject(new Walter(), x, y); } // in WALTER class // instance field private int counter; // in 'act' method runCounter(); // new method private void runCounter() { counter++; if (counter == 500) getWorld().removeObject(this); } // in MAIN actor class Walter walter = getOneIntersectingObject(Walter.class); if (walter != null) getWorld().removeObject(walter);