Hi! I'm currently having troubles with spawning enemies. I want enemies to spawn one by one every two seconds at random location but I can't seem to have it appear in the world.
This is my code currently:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
prepare();
spawnEnemy();
//spawnEnemyWave();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Player player = new Player();
addObject(player,400,300);
}
/*
public void spawnEnemyWave()
{
int enemySpawnTimer = 0;
int spawnEnemies = 0;
if (spawnEnemies > 0)
{
if (++enemySpawnTimer == 55*2)
{
enemySpawnTimer = 0;
spawnEnemies -= 2;
for (int i=0; i<2; i++)
{
ghost ghost = new ghost();
addObject(new ghost(), 50, 40);
}
}
}
else //no enemies will spawn in this wave anymore
{
if (getObjects(ghost.class).isEmpty()) //no enemies in the world
{
spawnEnemies = 2 * 25;
}
}
}
*/
public void spawnEnemy()
{
if(Greenfoot.getRandomNumber(120)<1)
//spawn random enemy every 2 seconds.
{
int spawnPlace = Greenfoot.getRandomNumber(8);
int ghostType = Greenfoot.getRandomNumber(50);
if(spawnPlace == 1)
{
if(ghostType <= 30)
{
ghost ghost = new ghost();
addObject(ghost, 50, 40);
}
else if(ghostType <= 45 && ghostType > 30)
{
fastGhost fastGhost = new fastGhost();
addObject(fastGhost,50,40);
}
else if(ghostType <= 50 && ghostType > 45)
{
bossGhost bossGhost = new bossGhost();
addObject(bossGhost, 50, 40);
}
}
else if(spawnPlace == 2)
{
if(ghostType <= 30)
{
ghost ghost = new ghost();
addObject(ghost, 150, 40);
}
else if(ghostType <= 45 && ghostType > 30)
{
fastGhost fastGhost = new fastGhost();
addObject(fastGhost,150,40);
}
else if(ghostType <= 50 && ghostType > 45)
{
bossGhost bossGhost = new bossGhost();
addObject(bossGhost, 150, 40);
}
}
else if(spawnPlace == 3)
{
if(ghostType <= 30)
{
ghost ghost = new ghost();
addObject(ghost, 250, 40);
}
else if(ghostType <= 45 && ghostType > 30)
{
fastGhost fastGhost = new fastGhost();
addObject(fastGhost,250,40);
}
else if(ghostType <= 50 && ghostType > 45)
{
bossGhost bossGhost = new bossGhost();
addObject(bossGhost, 250, 40);
}
}
else if(spawnPlace == 4)
{
if(ghostType <= 30)
{
ghost ghost = new ghost();
addObject(ghost, 350, 40);
}
else if(ghostType <= 45 && ghostType > 30)
{
fastGhost fastGhost = new fastGhost();
addObject(fastGhost,350,40);
}
else if(ghostType <= 50 && ghostType > 45)
{
bossGhost bossGhost = new bossGhost();
addObject(bossGhost, 350, 40);
}
}
else if(spawnPlace == 5)
{
if(ghostType <= 30)
{
ghost ghost = new ghost();
addObject(ghost, 450, 40);
}
else if(ghostType <= 45 && ghostType > 30)
{
fastGhost fastGhost = new fastGhost();
addObject(fastGhost,450,40);
}
else if(ghostType <= 50 && ghostType > 45)
{
bossGhost bossGhost = new bossGhost();
addObject(bossGhost, 450, 40);
}
}
else if(spawnPlace == 6)
{
if(ghostType <= 30)
{
ghost ghost = new ghost();
addObject(ghost, 550, 40);
}
else if(ghostType <= 45 && ghostType > 30)
{
fastGhost fastGhost = new fastGhost();
addObject(fastGhost,550,40);
}
else if(ghostType <= 50 && ghostType > 45)
{
bossGhost bossGhost = new bossGhost();
addObject(bossGhost, 550, 40);
}
}
else if(spawnPlace == 7)
{
if(ghostType <= 30)
{
ghost ghost = new ghost();
addObject(ghost, 650, 40);
}
else if(ghostType <= 45 && ghostType > 30)
{
fastGhost fastGhost = new fastGhost();
addObject(fastGhost,650,40);
}
else if(ghostType <= 50 && ghostType > 45)
{
bossGhost bossGhost = new bossGhost();
addObject(bossGhost, 650, 40);
}
}
else if(spawnPlace == 8)
{
if(ghostType <= 30)
{
ghost ghost = new ghost();
addObject(ghost, 750, 40);
}
else if(ghostType <= 45 && ghostType > 30)
{
fastGhost fastGhost = new fastGhost();
addObject(fastGhost,750,40);
}
else if(ghostType <= 50 && ghostType > 45)
{
bossGhost bossGhost = new bossGhost();
addObject(bossGhost, 750, 40);
}
}
}
}
}
