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

2012/11/10

How can I stop spawning enemies?

qwasyx qwasyx

2012/11/10

#
   public void enemySpawn()
    {
        if  (spawnCounter == 1000 )
        {
            getWorld().addObject(new Enemy1(), Greenfoot.getRandomNumber(900),Greenfoot.getRandomNumber(900));
            spawnCounter = spawnCounter -50;
            anzahlEnemies = anzahlEnemies+1;
            
            if (anzahlEnemies == 30 || 70 || 120 || 190 || 300 || 500)
            
            
        }  
        else
        {
            spawnCounter = spawnCounter +1;
        }
I want to stop spawning them if the number of enemies is about 30. How can I do that? Thanks! :)
SPower SPower

2012/11/10

#
if (getObjects(Enemy.class).size() == 30)
qwasyx qwasyx

2012/11/10

#
Cant I do this with line 09? My problem is that I dont know the method to stop it.
SPower SPower

2012/11/10

#
O, I'm sorry. Change line 9 to:
if (anzahlEnemies == 30 || 70 || 120 || 190 || 300 || 500) return;
'return' returns to the method which called this method.
qwasyx qwasyx

2012/11/10

#
It says: Bad operand types for binary operator "||" first type: boolean; second type: int
SPower SPower

2012/11/10

#
That's right, forgot to really read your line of code :) Do this:
if (anzahlEnemies == 30 ||
anzahlEnemies == 70 ||
anzahlEnemies == 120 ||
anzahlEnemies == 190 ||
anzahlEnemies == 300 ||
anzahlEnemies == 500) return;
but what would be easier, is to use a switch:
switch (anzahlEnemies) {
    case 30: case 70: case 120: case 190: case 300: case 500:
        return;
}
qwasyx qwasyx

2012/11/10

#
It doesnt work, I will show you the complete text. I dont know what is wrong.. Sorry I'm a beginner.
public class Spacko extends Actor
{
    int leben;
    int spawnCounter;
    int anzahlEnemies;

    public Spacko()
    {
        leben= 100;
        spawnCounter= 0;
        anzahlEnemies= 0;
    }

    public void act() 
    {
        bewegen();
        schiessen();
        enemySpawn();
    }

    private void schiessen()
    {
        if("space".equals(Greenfoot.getKey()))
        {

            Bullet bullet = new Bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.setRotation(getRotation());
        }
    }

    private void bewegen()
    {
        if (Greenfoot.isKeyDown("a"))
        {
            setRotation (getRotation()  -3);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            setRotation (getRotation() +3);
        }
        if (Greenfoot.isKeyDown("w"))
        {
            move(1);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            move(-1);        
        }    
    }

    public void enemySpawn()
    {
        if  (spawnCounter == 1000 )
        {
            getWorld().addObject(new Enemy1(), Greenfoot.getRandomNumber(900),Greenfoot.getRandomNumber(900));
            spawnCounter = spawnCounter -50;
            anzahlEnemies = anzahlEnemies+1;
            
            if (anzahlEnemies == 30) 
            return;  

        }
        else
        {
            spawnCounter = spawnCounter +1;
        }
    }

}
SPower SPower

2012/11/10

#
what doesn't work?
qwasyx qwasyx

2012/11/10

#
That the enemies wont spawn anymore after the anzahlEnemies reached 30.
SPower SPower

2012/11/10

#
No, that because you only do it when it's equal to 30, not equal and bigger. You replace line 60 with the code I gave you above or change it to:
if (anzalhEnemies >= 30)
which will return when anzahlEnemies is bigger or equal to 30
SPower SPower

2012/11/10

#
And I forgot something, you should move the code to the beginning of the method.
qwasyx qwasyx

2012/11/10

#
Thanks a bunch! :) Works.
You need to login to post a reply.