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

2018/8/9

actors spawning in on a repeating cycle

mole2003 mole2003

2018/8/9

#
Hi all, I am trying to make my World spawn enemies every 100 act cycles how can I do that?
Super_Hippo Super_Hippo

2018/8/9

#
//in the active world
private int spawnFrequency = 100, spawnTimer = spawnFrequency;

public void act()
{
    if (--spawnTimer == 0)
    {
        spawnTimer = spawnFrequency;
        addObject(new Enemy(), 50, 50);
    }
}
mole2003 mole2003

2018/8/10

#
The actor is not spawning?
Super_Hippo Super_Hippo

2018/8/10

#
Show your world class code.
mole2003 mole2003

2018/8/11

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    public static final int WIDE = 1182;
    public static final int HIGH = 432;
    private Scroller scroller;
    
    private int spawnFrequency = 100, spawnTimer = spawnFrequency;
    private int enemyClass = Greenfoot.getRandomNumber(5);
    public Background()
    {
        super(WIDE,HIGH,1,false);
        GreenfootImage image = new GreenfootImage("Background.png");
        scroller = new Scroller(this,image);

        prepare();
    }

    public void act()
    { 
        scroll();

    }

    

    private void scroll()
    {
        int speed = 2;
        
        scroller.scroll(speed, 0);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Trex trex = new Trex();
        addObject(trex,165,292);
        trex.setLocation(140,302);
        trex.setLocation(138,294);
        trex.setLocation(107,69);
       if (--spawnTimer == 0)
       {
         spawnTimer = spawnFrequency;
         if (enemyClass == 1)
         {
           addObject(new threeShort(), 1077, 325);
         }
         else if (enemyClass == 2)
         {
             addObject(new bird(), 1077, 325);
         }

       }
        
    }
}
Super_Hippo Super_Hippo

2018/8/11

#
The code has to be in the act method, not in the prepare method.
mole2003 mole2003

2018/8/11

#
once it has spawned in a new actor it only spawns a new actor. I would like that to change that so it spawns a random actor. Code I am using currently:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    public static final int WIDE = 1182;
    public static final int HIGH = 432;
    private Scroller scroller;
    
    private int spawnFrequency = 100, spawnTimer = spawnFrequency;
    private int enemyClass = Greenfoot.getRandomNumber(5);
    private int birdHeight = Greenfoot.getRandomNumber(3);
    public Background()
    {
        super(WIDE,HIGH,1,false);
        GreenfootImage image = new GreenfootImage("Background.png");
        scroller = new Scroller(this,image);

        prepare();
       
    }

    public void act()
    { 
        scroll();
         if (--spawnTimer == 0)
       {
         spawnTimer = spawnFrequency;
         if (enemyClass == 1)
         {
           addObject(new threeShort(), 1077, 325);
         }
         else if (enemyClass == 2)
         {
             if (birdHeight == 1)
             {
                 addObject(new bird(), 1077, 134);
             }
             else if (birdHeight == 2)
             {
                 addObject(new bird(), 1077, 213);
             }
             else if (birdHeight == 3)
             {
                 addObject(new bird(), 1077, 292);
             }
         }
         else if (enemyClass == 3)
         {
             addObject(new one(), 1077, 309);
         }
         else if (enemyClass == 4)
         {
             addObject(new threeTall(), 1077, 313);
         }
         else if (enemyClass == 5)
         {
             addObject(new two(), 10777, 310);
         }
       }
    }

    

    private void scroll()
    {
        int speed = 2;
        
        scroller.scroll(speed, 0);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Trex trex = new Trex();
        addObject(trex,165,292);
        trex.setLocation(140,302);
        trex.setLocation(138,294);
        trex.setLocation(107,69);
       
        
    }
}

Super_Hippo Super_Hippo

2018/8/11

#
The code in lines 17 and 18 are executed once, so it is one random number which will be used the whole game. Instead, you need to get the random number in the method, so it is a new number every time it is needed.
mole2003 mole2003

2018/8/11

#
how?
Super_Hippo Super_Hippo

2018/8/11

#
Move both lines (remove the "private" in the beginning) to between line 34 and 35.
You need to login to post a reply.