Hello,
i need help with my program... I want one object of my game to spawn randomly on the field an remove after 1 second and spawn again somewhere random.
Thanks.
public void act()
{
if(spawnTime == 0)
RandomSpawn();
else spawnTime--;
}
private void RandomSpawn()
{
addObject(new Mob(GetSpeed()),999,getRandomNumber(35,765));
spawnTime = 50;
}
private int getRandomNumber(int start, int range)
{
return start + (int)Greenfoot.getRandomNumber(range-start+1);
}
import greenfoot.*;
public class Peekaboo extends Actor
{
private int timer;
public void act()
{
timer = (timer+1)%60;
if (timer == 0)
{
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getHeight());
setLocation(x, y);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Die Welt besteht aus 29 * 17 Feldern.
*/
public class Ozean extends World
{
private static int zellenGroesse = 50;
private static int xmax = 29;
private static int ymax = 17;
/** p = plain, U = Uboot, T = Schatztruhe, W = Wasserstrudel, S = Seestern R = UbootRivale, N = Piranha */
private char[][] planetAufbau =
{
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
{'p','U','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','T'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','T'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','R','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
};
public Uboot uboot = new Uboot();
public UbootRivale ubootrivale = new UbootRivale();
/** Erschaffe eine Welt mit 29 * 17 Zellen. */
public Ozean()
{
super(xmax, ymax, zellenGroesse);
setBackground("images/wet-blue.jpg");
setPaintOrder(String.class, Uboot.class, UbootRivale.class, Schatztruhe.class, Seestern.class, Eisberg.class, Piranha.class);
setActOrder(Uboot.class, UbootRivale.class,Schatztruhe.class, Seestern.class,Eisberg.class, Piranha.class);
Greenfoot.setSpeed(28);
/** Die Welt mit Objekten bevölkern */
removeObjects(getObjects(null));
for (int py = 0; py <= ymax-1; py++)
{
for (int px = 0; px <= xmax-1; px++)
{
if (planetAufbau[py][px] == 'U')
{
Uboot uboot = new Uboot();
addObject(uboot, px, py);
}
if (planetAufbau[py][px] == 'R')
{
UbootRivale ubootrivale = new UbootRivale();
addObject(ubootrivale, px, py);
}
if (planetAufbau[py][px] == 'T')
{
Schatztruhe schatztruhe = new Schatztruhe();
addObject(schatztruhe, px, py);
}
if (planetAufbau[py][px] == 'S')
{
Seestern seestern = new Seestern();
addObject(seestern, px, py);
}
if (planetAufbau[py][px] == 'E')
{
Eisberg eisberg = new Eisberg();
addObject(eisberg, px, py);
}
if (planetAufbau[py][px] == 'N')
{
Piranha piranha = new Piranha();
addObject(piranha, px, py);
}
}
}
}
}
import greenfoot.*;
public class Peekaboo extends Actor
{
private int timer;
public void act()
{
timer = (timer+1)%60;
if (timer == 0)
{
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getHeight());
setLocation(x, y);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Seestern here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Seestern extends Actor
{
public Seestern()
{
setImage("images/Seestern3.png");
this.getImage().scale(50,50);
}
}public class Ozean extends World
{
private int spawnTime = 60,
wait = 0;
public void act()
{
if(wait == 0)
RandomSpawn();
else wait--;
}
private void RandomSpawn()
{
addObject(new Seestern(),getRandomNumber(0,xmax),getRandomNumber(0,ymax));
}
private int getRandomNumber(int start, int range)
{
return start + (int)Greenfoot.getRandomNumber(range-start+1);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Seestern extends Actor
{
private int wait = 60;
public Seestern()
{
setImage("images/Seestern3.png");
this.getImage().scale(50,50);
}
public void act()
{
if(wait == 0)
getWorld().removeObject(this);
else wait--;
}
}public class Ozean extends World
{
private int spawnTime = 60,
wait = 0,
counter = 0,
maxSeaStars = <yourLimit>;
public void act()
{
if(wait == 0)
RandomSpawn();
else wait--;
}
private void RandomSpawn()
{
if(counter < maxSeaStars)
{
addObject(new Seestern(),getRandomNumber(0,xmax),getRandomNumber(0,ymax));
counter++;
}
}
private int getRandomNumber(int start, int range)
{
return start + (int)Greenfoot.getRandomNumber(range-start+1);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Die Welt besteht aus 29 * 17 Feldern.
*/
public class Ozean extends World
{
private static int zellenGroesse = 50;
private static int xmax = 29;
private static int ymax = 17;
private int spawnTime = 60,
wait = 0;
private int counter = 0;
private int maxSeesterne = 10;
/** p = plain, U = Uboot, T = Schatztruhe, W = Wasserstrudel, S = Seestern R = UbootRivale, N = Piranha */
private char[][] planetAufbau =
{
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
{'p','U','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','T'},
{'p','p','p','p','p','p','p','p','p','p','p','p','S','p','p','p','p','p','p','p','p','p','p','p','p','p','S','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','T'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','S','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','R','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
{'p','p','p','p','p','p','p','p','p','p','p','S','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
{'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
};
public Uboot uboot = new Uboot();
public UbootRivale ubootrivale = new UbootRivale();
/** Erschaffe eine Welt mit 29 * 17 Zellen. */
public Ozean()
{
super(xmax, ymax, zellenGroesse);
setBackground("images/wet-blue.jpg");
setPaintOrder(String.class, Uboot.class, UbootRivale.class, Schatztruhe.class, Seestern.class, Eisberg.class, Piranha.class);
setActOrder(Uboot.class, UbootRivale.class,Schatztruhe.class, Seestern.class,Eisberg.class, Piranha.class);
Greenfoot.setSpeed(28);
/** Die Welt mit Objekten bevölkern */
removeObjects(getObjects(null));
for (int py = 0; py <= ymax-1; py++)
{
for (int px = 0; px <= xmax-1; px++)
{
if (planetAufbau[py][px] == 'U')
{
Uboot uboot = new Uboot();
addObject(uboot, px, py);
}
if (planetAufbau[py][px] == 'R')
{
UbootRivale ubootrivale = new UbootRivale();
addObject(ubootrivale, px, py);
}
if (planetAufbau[py][px] == 'T')
{
Schatztruhe schatztruhe = new Schatztruhe();
addObject(schatztruhe, px, py);
}
if (planetAufbau[py][px] == 'S')
{
Seestern seestern = new Seestern();
addObject(seestern, px, py);
}
if (planetAufbau[py][px] == 'E')
{
Eisberg eisberg = new Eisberg();
addObject(eisberg, px, py);
}
if (planetAufbau[py][px] == 'N')
{
Piranha piranha = new Piranha();
addObject(piranha, px, py);
}
}
}
}
public void act()
{
if(wait == 0)
RandomSpawn();
else wait--;
}
private void RandomSpawn()
{
if (counter < maxSeesterne)
{
addObject(new Seestern(),getRandomNumber(0,xmax),getRandomNumber(0,ymax));
counter++;
}
}
private int getRandomNumber(int start, int range)
{
return start + (int)Greenfoot.getRandomNumber(range-start+1);
}
}