This site requires JavaScript, please enable it in your browser!
Greenfoot back
Verglas
Verglas wrote ...

2019/6/1

Spawning Enemies One by One

Verglas Verglas

2019/6/1

#
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);
                    }
                }
            }
        
    }
}
danpost danpost

2019/6/1

#
Your call to spawnEnemy (line 21) is in the MyWorld constructor block. It is only executed once, when the world is being created. To have things happen continuously, you need to make use of an act method. FYI, the World class has one you can override (just like the Actor class).
danpost danpost

2019/6/1

#
You can reduce the size of your code immensely by making use of a little math:
import greenfoot.*;

public class MyWorld extends World
{
    public MyWorld()
    {
        super(800, 600, 1);
        prepare();
    }
    
    private void prepare()
    {
        addObject(new Player(), 400, 300);
    }
    
    public void act()
    {
        spawnEnemy();
    }
    
    private void spawnEnemy()
    {
        if (Greenfoot.getRandomNumber(120) < 1)
        {
            int spawnPlace = Greenfoot.getRandomNumber(8);
            int ghostType = Greenfoot.getRandomNumber(50);
            Actor ghost = null;
            if (ghostType < 30) ghost = new ghost();
            else if (ghostType < 45) ghost = new fastGhost();
            else ghost = new bossGhost();
            addObject(ghost, 50+100*spawnPlace, 40);
        }
    }
}
Verglas Verglas

2019/6/1

#
Thank you very much for the help! Works perfectly!
You need to login to post a reply.