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

2018/9/25

Increasing the size of the bubble by 10

gsingh87 gsingh87

2018/9/25

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


/**
 * A bit of empty space for bubbles to float in.
 * 
 * @author (Gurjeet Singh)
 * @version (22991720)
 */
public class Space extends World
{
  
    
    
    /**
     * Create Space. Make it black.
     */
    public Space()
    {
        super(900, 600, 1);
        getBackground().setColor(Color.BLACK);
        getBackground().fill();
        setup();
    }
    /**
     * Create 21 bubbles of random size diagonally
     */
    private void setup()
    {
       int i=0;
       while(i<21)
    {
        Bubble bubble = new Bubble();
        addObject(bubble,i*45,i*30);
        i++;
    }
    
    int a=0;
    while(a<10)
    {
        Bubbl bubble(a*10+10) = new Bubble();
        addObject(bubble(a*10+10),100+a*40,11);
        a++;
    }
}
}
gsingh87 gsingh87

2018/9/25

#
This shows me an error that bubble(int) not found. I want to increase the size of the bubbles by 10 every time in the second while loop
danpost danpost

2018/9/25

#
gsingh87 wrote...
This shows me an error that bubble(int) not found. I want to increase the size of the bubbles by 10 every time in the second while loop
You said in your other discussion thread that the size of the bubbles were to be random between 10 and 110. Your second while loop, first off, has no randomness in it and, secondly, does not have anything to do with the 21 bubbles you added into the world in your first while loop. It only attempts to add an extra 10 bubbles into the world with erroneous code. Remove the second loop. The sizing of each bubble should be done during the execution of the first while loop as each bubble is created. It can either be done right there in the setup method or in the Bubble class constructor (which is called from within the loop of the setup method.
gsingh87 gsingh87

2018/9/25

#
public class Bubble extends Actor
{
    private int speed;
    
    /**
     * Create a Bubble that floats, with random size and random color.
     */
    public Bubble()
    {
        // create a random size, between 10 and 110 pixels
        this(Greenfoot.getRandomNumber(100) + 10);
    }
    
    /**
     * Create a Bubble that floats, with a given size and random color.
     */
    public Bubble(int size)
    {
        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, size);
        setImage(img);
      
  
        
        
        // random speed: 1 to 4
        speed = Greenfoot.getRandomNumber(4) + 1;
        
        }
        
        
   
    
    
    /**
     * Create a Bubble that floats, with given size and initial float direction.
     */
    public Bubble(int size, int direction)
    {
        this(size);
        setRotation(direction);
    }
gsingh87 gsingh87

2018/9/25

#
I think I should change size in the second constructor to increase the size but not sure. Need a little help here Thanks for giving your time
danpost danpost

2018/9/25

#
gsingh87 wrote...
I think I should change size in the second constructor to increase the size but not sure. Need a little help here Thanks for giving your time
No. There should be no changing of size in the Bubble class (no individual bubble will be changing its size)-- just the initial setting of a given size. The Bubble class does not appear to need any modification. The second constructor can be called to create a bubble of a specified size from the setup method in your Space class.
gsingh87 gsingh87

2018/9/25

#
I think I have got the answer hopefully I execute it correctly. Thanks for the help
gsingh87 gsingh87

2018/9/25

#
It shows me an error may be I am executing wrong code in the setup method
danpost danpost

2018/9/25

#
gsingh87 wrote...
It shows me an error may be I am executing wrong code in the setup method
What error? What code?
gsingh87 gsingh87

2018/9/25

#
I am trying to call second constructor from the setup method but I do not know how to do this Sorry to be annoying but I am new to Greenfoot. add a second while loop to your setup() method that places some more bubbles. This loop should place 10 bubbles in a horizontal line, starting at x = 100, y = 11, with x increasing by 40 every time and y being constant. The size of the bubble should also increase, starting with 10 for the first bubble, then 20, then 30 etc. Use the second Bubble constructor for this. this is my exercise question
danpost danpost

2018/9/25

#
gsingh87 wrote...
I am trying to call second constructor from the setup method but I do not know how to do this
To call the second constructor, you would use:
Bubble bubble = new Bubble(<< size value >>);
Since the size will vary, you will need to use a variable there; or, at least some expression using a variable to have the value change each time it is called within the loop.
danpost danpost

2018/9/25

#
gsingh87 wrote...
This loop should place 10 bubbles in a horizontal line, starting at x = 100, y = 11, with x increasing by 40 every time and y being constant. The size of the bubble should also increase, starting with 10 for the first bubble, then 20, then 30 etc. Use the second Bubble constructor for this. this is my exercise question
Okay -- I understand why you had a second loop there now. It looks like you tried to put a coordinate value in for the size in your originally given code -- and you had it on the variable name and not in the constructor call.
gsingh87 gsingh87

2018/9/25

#
Exactly I really appreciate your effort
gsingh87 gsingh87

2018/9/26

#
What code should I use to increment the size of the bubbles by 10 every time
danpost danpost

2018/9/27

#
gsingh87 wrote...
What code should I use to increment the size of the bubbles by 10 every time
Initial size plus 'a' times 10.
You need to login to post a reply.