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

2017/6/20

How to randomly spawn actors?

Pammota Pammota

2017/6/20

#
I am new on this platform of creating games, and to java in general. I have worked in C++ for a year at school, and they do look similar, but I have some problems. I do not understand why the following code doesn't want to randomly spawn "worms" at random times across the map. Please help me understand what I have done wrong.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Worm here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Worm extends Actor
{
    /**
     * Act - do whatever the Worm wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       theyHatin();
       spawnal();
    }
    public void theyHatin()
    {   
        if(Greenfoot.getRandomNumber(100)<30)
        {
            move(Greenfoot.getRandomNumber(20));
        }
        
        if(Greenfoot.getRandomNumber(100)<5)
        {
            turn(Greenfoot.getRandomNumber(90));
        }
        
        if(getX() <= 5 || getX() >= getWorld().getWidth() - 5)
        {
            turn(180);
        }
        
        if(getY() <= 5 || getY() >= getWorld().getWidth() - 5)
        {
            turn(180);
        }
        
        
    }
    
    public void spawnal()
    {
    if(Greenfoot.getRandomNumber(100) == 1)
    {
    
    int x = Greenfoot.getRandomNumber(400);
    int y = Greenfoot.getRandomNumber(300);
    
    addObject(new Worm(), x, y);
}
    } 
    
}
davmac davmac

2017/6/20

#
As written, this should allow worms to spawn other worms (so the number of worms will increase at an exponential rate). If there are no worms to begin with, none will spawn. If you want worms to spawn when there are none, move the spawnal() method to your world class instead of having it in the Worm class. Also, as written, this won't compile:
addObject(new Worm(), x, y);
If you really do want to spawn worms from other worms, it should read:
getWorld().addObject(new Worm(), x, y);
(because addObject is a World method, not an Actor method).
Pammota Pammota

2017/6/21

#
Thanks a lot for pointing out what I have done wrong. What I want my code to do is to spawn worms at random times across the map, while the chance of spawning to remain constant. I have experimented a bit and I do not understand why my spawnal() method won't work in the world class, whether there are worms or not. I will leave the code here for you see what I have done wrong.
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 MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(484, 282, 1);

        prepare();
        spawnal();
        
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
                addObject(new urs(), 100,  100);
                 addObject(new Worm(), 300,  300);
                
                
            
    }
    public void spawnal()
    {
    if(Greenfoot.getRandomNumber(100) == 1)
    {
    
    int x = Greenfoot.getRandomNumber(400);
    int y = Greenfoot.getRandomNumber(300);
    
    addObject(new Worm(), x, y);
}
    } 
    
}
danpost danpost

2017/6/21

#
Line 22 will only be executed once during world creation because it is in the constructor of the class. The World class has an 'act' method just like that of the Actor class, but is not included in the class template (the initial code when you create a subclass of World). You can add it in:
public void act()
{
    spawnal();
}
and remove line 22. You could also remove line 33, if desired.
Pammota Pammota

2017/6/21

#
Omg, thanks so much, it finally works.
You need to login to post a reply.