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

2016/4/14

Bubbles Lab

MrsJones MrsJones

2016/4/14

#
Hi, New to Greenfoot. Trying to work through the practice and drill Bubble scenario. Im stuck on increasing the bubble size by 10 then 20, then 30 and so on. Any help would be appreciated.
danpost danpost

2016/4/14

#
If you refactor your code so that the creation of the image is dealt with individually:
private GreenfootImage createBubbleImage(int size, Color color)
{
   // code here
}
They you can use 'getWidth+10' and 'getColorAt' on the old image to get the values needed for the next image. You can use the method also to create its first image by getting a random color and using its original size (I would presume to be 10). You would probably then have a limit as to how big the image can be -- but that is another issue.
MrsJones MrsJones

2016/4/14

#
Hi, thank you for replying. We were able to create a loop that made 10 bubbles starting at X=300 and y=100 with x increasing by 40 each time and y remaining constant. The 10 bubbles increase in size starting at 10, then 20 and so on. So the last bubble should be 100 in size. We created our loop in the World subclass. Then the instructions state to use this constructor class to make the size changes. 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-1, size-1); setImage(img); // random speed: 1 to 4 speed = Greenfoot.getRandomNumber(4) + 1; } So your saying I need to edit the parameters
danpost danpost

2016/4/14

#
The "constructor class" does not necessarily mean the "constructor method". The constructor method is only executed once per instance created from the class; so, changing the size there only resets the initial size of the object. You cannot use the constructor method to alter the image after the actor is added into the world.
danpost danpost

2016/4/14

#
Wait, what exactly do the instructions say about creating bubbles that increase in size? (maybe I am confused as to what you are wanting here)
MrsJones MrsJones

2016/4/14

#
We created a method called setup() in our world subclass(Space). In this method we created a loop that makes 10 bubbles in a horizontal line, starting at x=300, y=100, with x increasing by 40 every time and y being constant. Here is the code for that below: public class Space extends World { public Space() { super(900, 600, 1); getBackground().setColor(Color.BLACK); getBackground().fill(); setUp(); } private void setUp() { int x=0; for(int number=0; number<10; number++) { Bubble bubble = new Bubble(); addObject(bubble, x, 100); x=x+40; } }} Then the directions read: the size of the bubble should also increase, starting with 10 for the first bubble, then 20, then 30. Use the second Bubble constructor for this. This is were we get stuck. Here is the code for the bubble class. 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-1, size-1); 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); } /** * Float. */ public void act() { if (isAtEdge()) { turn(180); } move(speed); if (Greenfoot.getRandomNumber(100) < 50) { turn(Greenfoot.getRandomNumber(5) - 2); // -2 to 2 } } }
danpost danpost

2016/4/14

#
Okay. I was thinking that each bubble would be increasing in size within the game; but, that is not the case here. You can use your 'for' loop index ( 'number' ) to set the appropriate size of each bubble created. The size would then be:
int size = 10+10*number;
// or
int size = (number+1)*10;
This 'size' will then be passed to the Bubble class to create a new bubble:
Bubble bubble = new Bubble(size);
MrsJones MrsJones

2016/4/14

#
Thank you that worked!
rc5pn rc5pn

2016/7/27

#
need some help implementing the code in the bubble constructor- couldn't use a for loop, instead used a while loop
danpost danpost

2016/7/27

#
rc5pn wrote...
need some help implementing the code in the bubble constructor- couldn't use a for loop, instead used a while loop
You should start a new discussion thread and reference this one for your issue. Could not use a for loop -- or not allowed to use a for loop? Show attempted code we can help you with. Explain why it does not meet your satisfaction.
You need to login to post a reply.