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

2020/2/6

Trying to make an actor that spawns randomly in my world

Adrian2004 Adrian2004

2020/2/6

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

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

    /**
     * Constructor for objects of class Snow.
     * 
     */
    public Snow()
    {    
        super(1265, 713, 1);
        
        System.out.println("World's constructor has been called.");
        
        Snowman snowman = new Snowman();
        addObject (snowman, 633, 357);
    public void act() {
       spawn();
       spawning();
    }
    public void spawn() {  
        
        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 spawning()
    {
    if(Greenfoot.getRandomNumber(100) == 1) {
     
    int x = Greenfoot.getRandomNumber(400);
    int y = Greenfoot.getRandomNumber(300);
     
    addObject(new point(), x, y);
}
    
I don't know why this doesn't work. Could someone help me out?
Super_Hippo Super_Hippo

2020/2/6

#
Move the code for spawning into the world. Otherwise, the code is executed for each Snow object. If there is none, it won’t ever be executed.
danpost danpost

2020/2/6

#
Your spawn method looks like it came out of an Actor subclass. It uses Actor class methods -- which won't work in a World subclass. Also, the code is for moving and turning -- the name, spawn, has nothing to do with that code. Remove line 25 and lines 28 thru 47.
Adrian2004 Adrian2004

2020/2/6

#
What do you mean? It's in the world. Sorry I'm a beginner.
danpost danpost

2020/2/6

#
Adrian2004 wrote...
What do you mean? It's in the world. Sorry I'm a beginner.
Well, you are showing the spawn method in your World subclass called Snow; however, a world does not turn or move and does not have coordinates of its own -- and you use the methods turn, move, getX and getY in the spawn method. These are Actor class methods and can only be used on Actor objects. Please refer to the Greenfoot API documentation to see what methods are available. All methods not declared static can only be used on instances of that class or one of its subclasses (static methods do not require any objects and can be called on the class itself).
danpost danpost

2020/2/6

#
Oh, your code is also not bracketed properly. Your constructor (block of code starting 'public Snow()') has an open curly bracket, but is missing its closing bracket ( '}' ) after line 23. There are also a couple of closing curly brackets missing at the end of the code shown.
Super_Hippo Super_Hippo

2020/2/6

#
Adrian2004 wrote...
What do you mean? It's in the world. Sorry I'm a beginner.
Oh wow, I was quickly going over the code and saw all that moving and turning that I instantly thought it had to be an Actor subclass..
You need to login to post a reply.