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

2016/11/21

Using a while loop to add bubbles in horizontal/diagonal line

georgette georgette

2016/11/21

#
Hello, I need help. I am trying to add a second while loop that places 10 bubbles in a horizontal line starting at x=300, y=100 with X increasing by 40 each loop and Y being constant. I included code from the world and the bubble, but I guess I should focus on using the second bubble constructor for this. I appreciate your feedback!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
     * Create a bubble.
     */
    private void createBubbles()
    {
        int i = 0;
        while (i < 11)
        {
            Actor bubble = new Bubble();
            this.addObject(new Bubble(), Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(600) );
            //i*107, 200);
            this.addObject(new Bubble(),this.getWidth() / 2, this.getHeight() / 2);
            //addObject (new Bubble(), 0, 30, 60, 90);
            i = i + 1;
        }
        int direction = 10;
        while (i < 10)
        {
            if ((direction < 0 && getX() == 0) || (direction > 0 && getX() == getWorld().getWidth()-1))
            direction = -direction;  move(direction);
        }
        //int worldWidth = this.getWidth() / 2,;
        //int worldHeight = this.getHeight();
 
    }
 
/**
     * 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);
    }
danpost danpost

2016/11/21

#
Let us walk through the 'createBubbles' method. Line 6 initializes an int variable, 'i', to zero; this variable will be used as a loop counter (no problem with that). Lines 7, 8, 14 and 15 (the 'while' loop structure) will cause the code inside the block to iterate 11 times (fine here). Now, for the code being executed 11 times (lines 9 through 13). Line 9 creates a Bubble object which is nowhere else referred to ('bubble', the name you gave to refer to it is never elsewhere used). Line 10 creates a second bubble and places in at some random location in the world. And, line 12 creates a third bubble and places it in the center of the world. So, you are adding 22 bubbles into the world, eleven of which are placed at the same center world location. The value of 'i' upon completing the first 'while' loop will be 11 and, therefore, the condition for the second loop is not right for it to execute any loops. You should be showing some errors on lines 19 and 20. 'getX()' and 'move(int)' are Actor class methods and cannot be used on a World object.
georgette georgette

2016/11/22

#
Thank you for your reply! In reference to lines 9 -13 I cleaned up my code. The first step requires 21 bubbles to placed in the center of the world using a while loop, the second step requires 21 bubbles placed at random locations in the world and then place the bubbles on a diagonal with x and y distances of 30. Eventually, the second loop should place more bubbles in a horizontal line starting at x=300, y=100. No luck yet getting the bubbles on a diagonal/horizontal line. I'll continue to work on it and will appreciate any ideas that'll get me headed in the right direction.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{
    private static final int GAP = 12;
 
    private Bubble bubble;
 
    /**
     * Create Space. Make it black.
     */
    public Space()
    {
        super(900, 600, 1);
        getBackground().setColor(Color.BLACK);
        getBackground().fill();    
 
        bubble = new Bubble();
        addObject( bubble, 30, 60);
 
        createBubbles();
    }
 
    /**
     * Create a bubbble.
     */
    private void createBubbles()
    {
        int i = 0;
        while (i < 10 )
        {
            Actor bubble = new Bubble();
            addObject(new Bubble(), Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(600) );
            addObject(new Bubble(),getWidth() / 2, getHeight() / 2);
            i = i + 1;
        }
 
        int x = 300;
        while (x < 11 )
        {   
            addObject( new Bubble(), x, 100);
            x = x + 40 + GAP;
        }
 
    }
 
}
danpost danpost

2016/11/22

#
You should not need a reference field for any of the bubbles (remove line 4); and the bubble created and added into the world with lines 15 and 16 are not part of any of your required set (remove those two lines). Still you are creating a bubble and not doing anything with it (line 29); remove that line. If you want to create 21 bubbles each at the center and at random locations, then change '10' in line 27 to '21'. The second while loop again will never execute. You initialize x to '300' a value greater than '11', which the loop requires the value of x to be less than.
You need to login to post a reply.