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

2013/6/9

How to increase enemy spawner

SimStaplerPro SimStaplerPro

2013/6/9

#
So, in my game I have balloons that fly from right to left onscreen, and a scoreboard that keeps track of the amount of times the act method has occurred and displays it onscreen. If I wanted to make it so when the score hit 2000, the frequency of enemies would increase, how would I go about doing it? The spawner is a method in the world class with the following code:
/**
     * The method used to spit out balloons every so often.
     */
    public void spawnBalloons()  {
        
        if(Greenfoot.getRandomNumber(900) <= 10)
            addObject(new RedBalloon(), 790, Greenfoot.getRandomNumber(200));
        if(Greenfoot.getRandomNumber(300) <= 10)
            addObject(new BlueBalloon(), 790, Greenfoot.getRandomNumber(200));
        if(Greenfoot.getRandomNumber(600) <= 10)
            addObject(new YellowBalloon(), 790, Greenfoot.getRandomNumber(200));
        
    }
danpost danpost

2013/6/9

#
Right now, you have a hard-coded '10' to regulate the rate of spawning. Create an instance field in the world class to hold the value and change its value to increase the spawn rate.
// add instance field
private spawnChance = 10;
// change conditions to '<= spawnChance', and
// add the following to the act method 
// (or method it calls) in world class
if (spawnChance < 20 && /* get score */ >= 2000) spawnChance = 20;
SimStaplerPro SimStaplerPro

2013/6/9

#
Ahh, I see. I tried something similar, but the one problem is I can't seem to call my getScore() method, it always says it can't recognize it. The code is in the ScoreBoard class, I don't know if that has anything to do with me not being able to use it in my world class
danpost danpost

2013/6/9

#
The ScoreBoard class supplied with Greenfoot is used as a final score display that shows the top final scores and final scores near the user's final score. It is not for keeping the current score throughout the game. The Counter class could be used for that (this class does contain a method called 'getValue').
SimStaplerPro SimStaplerPro

2013/6/9

#
Where would I be able to get the code for this counter class? Is there a link to a scenario that uses it?
danpost danpost

2013/6/10

#
Select 'Edit'>'Import Class...' on the menubar and choose the 'Counter' class and click on the 'Import' button.
SimStaplerPro SimStaplerPro

2013/6/10

#
Thank you very much!
You need to login to post a reply.