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

2019/4/25

Need help making bubbles appear on diagonal line & move from top left corner to bottom right & the last bubble end at coordinates 900,600.

Dexadashd Dexadashd

2019/4/25

#
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
{
    private int bubbleSize;
    private int bubbleDirection;
    /**
     * Create Space. Make it black.
     */
    public Space()
    {
        super(900, 600, 1);
        getBackground().setColor(Color.BLACK);
        getBackground().fill();
        prepare();
        setup();
    }

    private void setup()
    {
        int i=0;
        while (i<18)
        {
            Bubble bubble = new Bubble();
            addObject(new Bubble(190-10*i,i*20),getWidth()/2,getHeight()/2);
            i++;
        }

        while (i < 10 )
        {
            Actor bubble = new Bubble();
            addObject(new Bubble(), 900, 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;
        }
        
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Bubble bubble = new Bubble();
        addObject(bubble,140,152);
        Bubble bubble2 = new Bubble();
        addObject(bubble2,185,243);
        Bubble bubble3 = new Bubble();
        addObject(bubble3,462,359);
        Bubble bubble4 = new Bubble();
        addObject(bubble4,661,209);
        Bubble bubble5 = new Bubble();
        addObject(bubble5,690,370);
        Bubble bubble6 = new Bubble();
        addObject(bubble6,658,514);
        Bubble bubble7 = new Bubble();
        addObject(bubble7,380,216);
        Bubble bubble8 = new Bubble();
        addObject(bubble8,744,90);
        Bubble bubble9 = new Bubble();
        addObject(bubble9,780,367);
        Bubble bubble10 = new Bubble();
        addObject(bubble10,319,489);
        Bubble bubble11 = new Bubble();
        addObject(bubble11,166,343);
        Bubble bubble12 = new Bubble();
        addObject(bubble12,282,334);
        Bubble bubble13 = new Bubble();
        addObject(bubble13,603,212);
        Bubble bubble14 = new Bubble();
        addObject(bubble14,536,76);
        Bubble bubble15 = new Bubble();
        addObject(bubble15,519,376);
        Bubble bubble16 = new Bubble();
        addObject(bubble16,577,504);
        Bubble bubble17 = new Bubble();
        addObject(bubble17,870,542);
        Bubble bubble18 = new Bubble();
        addObject(bubble18,867,239);
        Bubble bubble19 = new Bubble();
        addObject(bubble19,244,113);
        Bubble bubble20 = new Bubble();
        addObject(bubble20,426,512);
        Bubble bubble21 = new Bubble();
        addObject(bubble21,78,501);
    }
}
danpost danpost

2019/4/25

#
Certainly, you do not want to multiply at line 48.
Dexadashd Dexadashd

2019/4/26

#
updated it to x=x+40. still not able to get it to a diagonal line and move from top c\left corner to bottom right corner but i suspect i need to use the isMouseClicked method. Just not sure how to properly write it out.
danpost danpost

2019/4/26

#
With a rotation of 33 degrees, moving at a rate of 4 pixels per act, an actor will take a direct route between the two corners. To place an actor randomly, but exactly on the diagonal, take any random value less than 300 and multiply it by 3 for the x and multiply it by 2 for the y, to be where to add the actor into the world. Keep in mind that the actor will never be at exactly (900, 600) as the lower right corner is at (899, 599).
You need to login to post a reply.