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

2017/7/23

Can't figure out error or even see what it is

DankBoi DankBoi

2017/7/23

#
I am pretty new to Greenfoot and programming in general, and I've been taking a class to improve my skills. One of the assignments involves drawing variously colored and positioned circles and different places in a world using while loops. I wrote the following code and whenever it's put in the constructor method I get some kind of terminal error. However, I can't even see the terminal or find the greenfoot.properties file to change its location.
public void setup34()
    {
        int i = 0;
        while (i < 20)
        {
            addObject(new Bubble(190 - i * 10), getWidth()/2, getHeight()/2);
        }
    }
This is the code for the entire world and the bubble class, in case that's helpful. World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * A bit of empty space for bubbles to float in.
 * 
 * @author Michael Kölling
 * @version 1.0
 */
public class Space extends World
{
    /**
     * Create Space. Make it black.
     */
    public Space()
    {
        super(900, 600, 1);
        getBackground().setColor(Color.ORANGE);
        getBackground().fill();
        //setup29();
        //setup30();
        setup34();
    }
    
    public void setup28()
    {
        addObject(new Bubble(), getWidth()/2, getHeight()/2); // NOTE:  use getWidth()/2 and getHeight()/2 for the center        
    }
    
    public void setup29()
    {
        int i = 0;
        while (i < 29)
        {
            setup28();
            i++;
        }
    }
    
    public void setup30()
    {
        int i = 0;
        while (i < 30)
        {
            addObject(new Bubble(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
            i++;
        }
    }
    
    public void setup31()
    {
        int i = 0;
        while (i < 21)
        {
            addObject(new Bubble(),i * 30, i * 30);
            i++;
        }
    }
    
    public void setup32()
    {
        int i = 0;
        while (i < 21)
        {
            addObject(new Bubble(),i * 45, i * 30);
            i++;
        }
    }
    
    public void setup33()
    {
        int i = 0;
        while (i < 10)
        {
            addObject(new Bubble(i*10 + 10),i * 40 + 300, 100);
            i++;
        }
        
        int j = 0;
        while (j < 21)
        {
            addObject(new Bubble(),j * 45, j * 30);
           j++;
        }
       
        
    }
    
    public void setup34()
    {
        int i = 0;
        while (i < 20)
        {
            addObject(new Bubble(190 - i * 10), getWidth()/2, getHeight()/2);
        }
    }
    
}
Bubble class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * This is a colored floating bubble.
 *
 * @author Michael Kölling
 * @version 1.0
 */
public class Bubble extends Actor
{
    private int speed;      // assigned to be a random number in the constructor method and used in the act method
    
    /**
     * Create a Bubble that floats, with random size and random color.
     */
    public Bubble()         // first constructor, the default constructor with no argumens         
    {
        // create a random size, between 10 and 110 pixels
        this(Greenfoot.getRandomNumber(100) + 10);      // recall this is the Java name of ourself, will call the second constructor
    }
    
    /**
     * Create a Bubble that floats, with a given size and random color.
     */
    public Bubble(int size)       // second constructor, the size of the bubble is supplied as a paramenter
    {
        GreenfootImage img = new GreenfootImage(size, size);

        // create a random color, with every color channel between 30 and 230
        int red = Greenfoot.getRandomNumber(200) + 30;
        int green = Greenfoot.getRandomNumber(200) + 30;
        int blue = Greenfoot.getRandomNumber(200) + 30;
        int alpha = Greenfoot.getRandomNumber(190) + 60;
        
        img.setColor(new Color(red, green, blue, alpha));
        img.fillOval(0, 0, size-1, size-1);
        setImage(img);               // IMPORTANT CONCEPT, the image dynamically created above now becomes the image
                                      //     for this particular object at run time when it is created.
                                      //  NOTE:  No image is assigned as the default image for the Class
        
        // random speed: 1 to 4
        speed = Greenfoot.getRandomNumber(4) + 1;     // the private instance field speed is given a value  in the constructor
    }
    
    /**
     * Create a Bubble that floats, with given size and initial float direction.
     */
    public Bubble(int size, int direction)    // third constructor, both size and direction are specified as parameters
    {
        this(size);            // IMPORTANT CONCEPT, just as the first constructor calls the second constructor, so does the
                               //             third constructor call the second constructor.  That way the code to dynamically create
                               //             the individual image for the object needs to be given in only 1 place, the second constructor
        setRotation(direction);
    }
    
    /**
     * Float.
     */
    public void act() 
    {
        if (isAtEdge()) {
            turn(180);
        }
        
        move(speed);
        
        if (Greenfoot.getRandomNumber(100) < 50) {            // randomly turn 1/2 the time
            turn(Greenfoot.getRandomNumber(5) - 2);   // -2 to 2
        }
    }
}
danpost danpost

2017/7/23

#
You are not incrementing the counter in the 'setup34' loop.
DankBoi DankBoi

2017/7/24

#
I added an i++; statement, and it still doesn't work. It doesn't make any sense.
danpost danpost

2017/7/25

#
In the Space class, in the setup34 method, change the '10' in the 'new Bubble' parameter expression to '9' (or change the '190' to '210'). When 'i' reaches 20, 190-i*10 ends up to be a negative number which you cannot use for a dimension of an image.
DankBoi DankBoi

2017/7/25

#
I see it now. I fixed it by changing the condition from i<20 to i<19. It works now. Thank you!
You need to login to post a reply.