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

2014/3/26

Limiting how many enemies spawn

Ghost_Nixon Ghost_Nixon

2014/3/26

#
I was able to spawn my enemies into the game (Thank you to the people that helped) but now only a few will spawn and then a massive horde of enemies will follow and I don't want that to happen.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        spawning();
        setLocation(getX()-3, getY());
        endgame();
        if (getWorld() != null)despawn();
        
         
        
        
        
        }  
    
        /**
         *  Spawns the enemy on the edge of the world randomly
         *  
         */
        public void spawning()
        {
            
            
            
            if(Greenfoot.getRandomNumber(900) <= 11)
            {
                 getWorld().addObject(new Enemy(),599,Greenfoot.getRandomNumber(400));
        }
        
      }
Thank you in advance!
danpost danpost

2014/3/26

#
Change line 37 to:
Ghost_Nixon Ghost_Nixon

2014/3/27

#
It doesn't actually recognize .size as a variable.
danpost danpost

2014/3/27

#
This is what I meant: Change line 37 to:
if (getWorld().getObjects(Enemy.class).size() < /** maximum number of enemies */ && Greenfoot.getRandomNumber(900) <= 11)
You need to login to post a reply.