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

2017/3/21

My zombies won't spawn!

Grace20102 Grace20102

2017/3/21

#
public class Game extends World
{
     Dave dave = new Dave();
     Zombie zombie = new Zombie();
     Score score = new Score();
     public Game()
     {    
        super(600, 400, 1); 
        addObject(dave, 20,345);
        addObject(score, 300,15);
     }
     public Score getScore()
     {
        return score;
     }
     private int Timer;
     private void runTimer()
     {
         Timer = (Timer+1)%200;
         if (Timer == 0) spawnZombie();
     }
     private void spawnZombie()
     {
        Zombie[] zombies = new Zombie[5];
        for(int i=0; i<zombies.length; i++)
        {
            zombies[i] = new Zombie();
            int zombieX = Greenfoot.getRandomNumber(getWidth());
            int zombieY = (345);
            addObject(zombies[i], zombieX, zombieY);
        }
     }
}  
This is the code and they just won't spawn in!
Nosson1459 Nosson1459

2017/3/22

#
With your shown code there is no place that an addObject for a zombie gets executed. The only place that there is one is in the spawnZombie method which gets called in the runTimer method which doesn't get called (in the act). Your complete Game class should be:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Game here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class Game extends World
{
    private Dave dave = new Dave();
    private Zombie zombie = new Zombie();
    private Score score = new Score();
    private int timer;

    /**
     * Constructor for objects of class Game.
     * 
     */
    public Game()
    {
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        addObject(dave, 20,345);
        addObject(score, 300,15);
    }
    
    /**
     * Act - do whatever the Game wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        runTimer();
    }

    private void runTimer()
    {
        timer = (timer + 1) % 200;
        if (timer == 0) spawnZombie();
    }

    public Score getScore()
    {
        return score;
    }

    private void spawnZombie()
    {
        Zombie[] zombies = new Zombie[5];
        for (int i = 0; i < zombies.length; i++)
        {
            zombies[i] = new Zombie();
            int zombieX = Greenfoot.getRandomNumber(getWidth());
            int zombieY = 345;
            addObject(zombies[i], zombieX, zombieY);
        }
    }
}
Grace20102 Grace20102

2017/3/22

#
Nosson1459 wrote...
With your shown code there is no place that an addObject for a zombie gets executed. The only place that there is one is in the spawnZombie method which gets called in the runTimer method which doesn't get called (in the act). Your complete Game class should be:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Game here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class Game extends World
{
    private Dave dave = new Dave();
    private Zombie zombie = new Zombie();
    private Score score = new Score();
    private int timer;

    /**
     * Constructor for objects of class Game.
     * 
     */
    public Game()
    {
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        addObject(dave, 20,345);
        addObject(score, 300,15);
    }
    
    /**
     * Act - do whatever the Game wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        runTimer();
    }

    private void runTimer()
    {
        timer = (timer + 1) % 200;
        if (timer == 0) spawnZombie();
    }

    public Score getScore()
    {
        return score;
    }

    private void spawnZombie()
    {
        Zombie[] zombies = new Zombie[5];
        for (int i = 0; i < zombies.length; i++)
        {
            zombies[i] = new Zombie();
            int zombieX = Greenfoot.getRandomNumber(getWidth());
            int zombieY = 345;
            addObject(zombies[i], zombieX, zombieY);
        }
    }
}
Thank you so much! It works now!
You need to login to post a reply.