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

2016/11/29

My bubble scenario

DocWhiskey DocWhiskey

2016/11/29

#
Hello everyone I was wondering if someone can help me out here. I'm working on a scenario involving bubbles, if you have the book it is Chapter 6 pg. 121. I am working on the part where I am supposed to increase the bubbles that are running horizontal by 10 in some sort of loop until it stops at 100 starting at 10 for the first bubble, 20, 30, 40, etc. I feel like i know what to do but everything i have tried does not work.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * 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.BLACK);
        getBackground().fill();
    setup();
    
}
     private void setup ()
     
{
    Bubble[] bubble = new Bubble[21];
    int i = 0;
 
    while (i < bubble.length)
    {
        bubble[i] = new Bubble();
        addObject(bubble[i], i*45, i*30);
        i++;
    }
    int x = 0; 
 
    while ( x < 10 )                   
    {
        addObject(new Bubble(),300 + x +40, 100);
        x = x + 1;                 
    }
int x = 0; is where the horizontal line starts so if anyone can help me out that would be great
Super_Hippo Super_Hippo

2016/11/29

#
Try to multiply x by something like it is done in line 34 but only for the x coordinate.
DocWhiskey DocWhiskey

2016/12/1

#
yeah i tried that but it didn't really work. I just ended up getting these oversized bubbles all bunched up together.
danpost danpost

2016/12/1

#
DocWhiskey wrote...
yeah i tried that but it didn't really work. I just ended up getting these oversized bubbles all bunched up together.
What, exactly, did you try? post the code you tried.
DocWhiskey DocWhiskey

2016/12/1

#
int x = 0; 
  
    while ( x < 10 )                   
    {
        addObject(new Bubble(10 +10 *2),300 + x +40, 100);
        x = x + 1;                 
    }
there it is. Someone told me that you have to give the bubble a value or else they are all gonna come out the same size but they are coming out the same size anyways haha. I know the above code is not right but it is what i tried.
DocWhiskey DocWhiskey

2016/12/1

#
DocWhiskey wrote...
int x = 0; 
  
    while ( x < 10 )                   
    {
        addObject(new Bubble(10 +10 *2),300 + x +40, 100);
        x = x + 1;                 
    }
there it is. Someone told me that you have to give the bubble a value or else they are all gonna come out the same size but they are coming out the same size anyways haha. I know the above code is not right but it is what i tried.
I think i figured it out
int x = 0; 
 
    while ( x < 10)                   
    {
        addObject(new Bubble (x * 10 + 1),300 + x*40, 100);
        x = x + 1;                 
    }
danpost danpost

2016/12/1

#
DocWhiskey wrote...
I think i figured it out < Code Omitted >
That is very close. I think you want to add '10', not '1', in the Bubble parameter; so that the first bubble will not be pixel-sized.
DocWhiskey DocWhiskey

2016/12/1

#
Yep i got it, thanks alot guys!
You need to login to post a reply.