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

2020/6/2

Help with arrays and for loop

1
2
danpost danpost

2020/6/2

#
Sammyy2323 wrote...
ok, and I would keep the CreateBalls method and then make the Ball method also? (sorry for asking questions, final project is due in like 2 days and I need to make sure this is correct)
"Ball method"? No -- Ball class extending Actor.
danpost danpost

2020/6/2

#
I just noticed a flaw in my line 5 insertion code It should be:
Ball ball = new Ball(i);
Sammyy2323 Sammyy2323

2020/6/2

#
Am i on the right track for doing this?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball0 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball0 extends Actor
{
    private static final String [] Colors = {"RED", "BLUE", "GREEN", "YELLOW"};
    private int value;
    Ball0 ball = new Ball0(i);
    public Ball0(int id)
    {
        value = id;
        setImage(new GreenfootImage(Colors[value] +"-ball.png"));
    }
    public String getColor()
    {
        return Colors[value];
    }
    
    public void CreateBalls()
    {
        int [] num = new int [4];
        num [0] = 5+Greenfoot.getRandomNumber(6);
        num [1] = 5+Greenfoot.getRandomNumber(6);
        num [2] = 5+Greenfoot.getRandomNumber(6);
        num [3] = 5+Greenfoot.getRandomNumber(6);
        
        for (int i=0; i<num.length; i++)
        
        {
            
            for (int n=0; n<num[i]; n++)
            {
                
            }
            
        }
        
    }
}
Sammyy2323 Sammyy2323

2020/6/2

#
am i supposed to add the addObject in the inner loop?
danpost danpost

2020/6/2

#
Sammyy2323 wrote...
am i supposed to add the addObject in the inner loop?
Yes. Line 13 is misplaced, as it should be before the addObject line.
danpost danpost

2020/6/2

#
Hold on here. Why did you put it all in the Ball0 class? The CreateBalls method needs to be in your World subclass.
Sammyy2323 Sammyy2323

2020/6/2

#
Ok i moved it into the World class but on the addObject line it is giving me an error for Ball0 saying I didn't define the variable?
 public void CreateBalls()
    {
        int [] num = new int [4];
        num [0] = 5+Greenfoot.getRandomNumber(6);
        num [1] = 5+Greenfoot.getRandomNumber(6);
        num [2] = 5+Greenfoot.getRandomNumber(6);
        num [3] = 5+Greenfoot.getRandomNumber(6);
        
        for (int i=0; i<num.length; i++)
        
        {
            
            for (int n=0; n<num[i]; n++)
            {
                Ball0 ball = new Ball0(i);
                addObject(Ball0, 300, 200);
            }
            
        }
danpost danpost

2020/6/2

#
The variable (reference) name is ball -- not Ball0.
Sammyy2323 Sammyy2323

2020/6/2

#
Ok i just ran everything and there were no errors, however, nothing is being created, is there a way that there is an example to see of what I'm doing wrong?
danpost danpost

2020/6/3

#
Sammyy2323 wrote...
Ok i just ran everything and there were no errors, however, nothing is being created, is there a way that there is an example to see of what I'm doing wrong?
Show code in world class. Do you have ball images in your images folder of this scenario or are you to create those images thru code?
Sammyy2323 Sammyy2323

2020/6/3

#
I imported a red ball and named it "RED-ball.png"
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
    }
    
    public void CreateBalls()
    {
        int [] num = new int [4];
        num [0] = 5+Greenfoot.getRandomNumber(6);
        num [1] = 5+Greenfoot.getRandomNumber(6);
        num [2] = 5+Greenfoot.getRandomNumber(6);
        num [3] = 5+Greenfoot.getRandomNumber(6);
        
        for (int i=0; i<num.length; i++)
        
        {
            
            for (int n=0; n<num[i]; n++)
            {
                Ball0 ball = new Ball0(i);
                addObject(ball, 300, 200);
            }
            
        }
        
Sammyy2323 Sammyy2323

2020/6/3

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

/**
 * Write a description of class Ball0 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball0 extends Actor
{
    private static final String [] Colors = {"RED", "BLUE", "GREEN", "YELLOW"};
    private int value;
    public Ball0(int id)
    {
        value = id;
        setImage(new GreenfootImage(Colors[value] +"-ball.png"));
    }
    public String getColor()
    {
        return Colors[value];
    }
    
    
}
danpost danpost

2020/6/3

#
Sammyy2323 wrote...
I imported a red ball and named it "RED-ball.png"
You will need to do the same for the other colored balls. You will also need to call the CreateBalls method from the prepare method.
Sammyy2323 Sammyy2323

2020/6/3

#
im not sure but i think i got it to work, if I have any issues then I will refer back to this thread. Thanks a lot man!
You need to login to post a reply.
1
2