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

2016/2/25

help with spawn

khoalabear khoalabear

2016/2/25

#
I was able to get my actors to spawn on the left side and then move to the right side but I seem to have messed up somewhere and it does not work now. Here is the code. I would also like to know how I can put a limit on how many actors can spawn thanks.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Pond_world extends World
{
    public Pond_world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1440, 810, 1); 
        prepare();
    }

    public void prepare()
    {
        User_fish user_fish = new User_fish();
        addObject(user_fish,1152,313);
        user_fish.setLocation(770,405);
    }

    private int spawnCounter = 0;
 
    public void Right_count() 
    {
        if (spawnCounter > 100) 
        {
            spawnCounter = 0;
            addObject(new AI_right(),0 , Greenfoot.getRandomNumber(getHeight()));
        }
        spawnCounter++;
    }
}
Vellyxenya Vellyxenya

2016/2/25

#
Hi, you must write the "act" method and call your Right-count() method from there like this:
public void act()
{
      Right-count();
}
khoalabear khoalabear

2016/2/25

#
Oh thanks so much. I must have deleted it before. Do you know how I can add a limit on how many actors are spawned?
moobe moobe

2016/2/25

#
You create a counter, which counts whenever an actor is spawned. Before the new actor is spawned, you ask: Is the counter < 3? If yes, spawn actor.
Vellyxenya Vellyxenya

2016/2/26

#
And don't forget to write "counter--" whenever you destroy the enemy, or it exits the World
You need to login to post a reply.