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

2015/4/7

Stop Spawn Timer

Programmer1998 Programmer1998

2015/4/7

#
I'm trying to get my other enemies to stop spawning once the Boss is spawned. Not sure how, any help is appreciated!
public class MarioWorld extends World
{
    private GreenfootSound Music = new GreenfootSound("bgmusic.mp3");
    private GreenfootSound bossMusic = new GreenfootSound("bossfight.mp3");
    private int spawnTimer_;
    ScoreBoard board = new ScoreBoard();
    Lives lives = new Lives();
    HealthBar bar = new HealthBar();
    /**
     * Constructor for objects of class MarioWorld.
     * 
     */
    public MarioWorld()
    {    
        // Create a new world with 1000x680 cells with a cell size of 1x1 pixels.
        super(1000, 680, 1); 
        
        Mario hero = new Mario();
        addObject(hero, getWidth()/2, getHeight()/2);
        
        Lakitu player = new Lakitu(0.005);
        addObject(player, 26, 35);
        
        addObject(board, 876, 36);
        
        addObject(lives, 92, 647);
        
        spawnTimer_ = 0;
    }

    /**
     * @return ScoreBoard
     */
    public ScoreBoard getBoard()
    {
        return board;
    }
    
    /**
     * @return Lives
     */
    public Lives getLives()
    {
        return lives;
    }
    
    /** 
     * @return HealthBar
     */
    public HealthBar getBar()
    {
        return bar;
    }
    
    /**
     * Plays the music when the game is running.
     */
    public void started()
    {
        Music.playLoop();
    }
    
    /**
     * Stops the music when the game is stopped.
     */
    public void stopped()
    {
        Music.stop();
        bossMusic.stop();
    }
    
    public void act()
    {
        if(spawnTimer_ == 0) // same concept as reloadTimer_
        {
            spawnCheepCheep();
            spawnGreenCC();
        }
        else
        {
            spawnTimer_ --;
        }
        
        spawnBoss();
    }

    /**
     * Spawns the boss when Mario has collected 20 coins.
     * Adds in the health bar when Mario has collected 20 coins.
     * Starts the new music, and ends the old one.
     */
    public void spawnBoss()
    {
        if(Mario.coinsCollected_ >= 20)
        {
            addObject(bar, 552, 127);
            addObject(new Boss(), 1000, 340);
            bossMusic.playLoop(); //starts the new music 
            Music.stop(); //stops the original music to play bossfight music   
        }
    }
    
    /**
     * Spawns the amount of CheepCheep depending on the amount of coins Mario has collected.
     * Difficulty increases(more CheepCheep spawned) when Mario collectes 15 or more coins.
     */
    public void spawnCheepCheep()
    {
        //if Mario collects less than 15 coins, spawn 2 CC each time
        if(Mario.coinsCollected_ < 15)
        {
            CheepCheep enemy = new CheepCheep();
            CheepCheep enemy2 = new CheepCheep();
            addObject(enemy, 0, Greenfoot.getRandomNumber(496)+92);
            addObject(enemy2, 0, Greenfoot.getRandomNumber(496)+92);
            spawnTimer_ = 250;
        }
        //if Mario collects 15 or more coins, start spawning an extra CC
        //difficulty increases!
        else if(Mario.coinsCollected_ >= 15)
        {
            CheepCheep enemy = new CheepCheep();
            CheepCheep enemy2 = new CheepCheep();
            CheepCheep enemy3 = new CheepCheep();
            addObject(enemy, 0, Greenfoot.getRandomNumber(496)+92);
            addObject(enemy2, 0, Greenfoot.getRandomNumber(496)+92);
            addObject(enemy3, 0, Greenfoot.getRandomNumber(496)+92);
            spawnTimer_ = 300;
        }
    }
    
    /**
     * Spawns the amount of GreenCC depending on the amount of coins Mario has collected.
     * Difficulty increases as Mario collected 15 or more coins.
     */
    public void spawnGreenCC()
    {
        // if Mario has less than 15 coins collected spawnn 2 CC at once
        if(Mario.coinsCollected_ < 15)
        {
            GreenCC enemy3 = new GreenCC();
            GreenCC enemy4 = new GreenCC();
            addObject(enemy3, 1000, Greenfoot.getRandomNumber(496)+92);
            addObject(enemy4, 1000, Greenfoot.getRandomNumber(496)+92);
            spawnTimer_ = 200;
        }
        //if Mario collects 15 or more coins, then the number of CC spawns increases by 1
        else if(Mario.coinsCollected_ >= 15) 
        {
            GreenCC enemy3 = new GreenCC();
            GreenCC enemy4 = new GreenCC();
            GreenCC enemy5 = new GreenCC();
            addObject(enemy3, 1000, Greenfoot.getRandomNumber(496)+92);
            addObject(enemy4, 1000, Greenfoot.getRandomNumber(496)+92);
            addObject(enemy5, 1000, Greenfoot.getRandomNumber(496)+92);
            spawnTimer_ = 300;
        }
    }
}
danpost danpost

2015/4/7

#
The trigger to spawn the boss is that Mario has collected 20 coins. Change lines 120 and 148 to:
else if (Mario.coinsCollected_ >= 15 && Mario.coinsCollected_ < 20)
danpost danpost

2015/4/7

#
You can change line 94 to this:
if (Mario.coinsCollected_ >= 20 && getWorld().getObjects(Boss.class).isEmpty())
so you do not continually spawn boss after boss.
Programmer1998 Programmer1998

2015/4/7

#
Thanks so much! For where you said to "change line 94 to this: " , I'm afraid what you wrote does not appear on my screen. Can you repost perhaps? Thanks!
danpost danpost

2015/4/7

#
Programmer1998 wrote...
Thanks so much! For where you said to "change line 94 to this: " , I'm afraid what you wrote does not appear on my screen. Can you repost perhaps? Thanks!
Just click on 'Quote' to view the text (with tags).
You need to login to post a reply.