Hello. I am to spawn in a bunch of colored balls that will fall from the top of the screen through a file dialog approach.
The file contains this code with correlates the ball's color when it will be spawned in:
10 10 10
255 0 0
0 255 0
0 0 255
125 255 125
200 10 100
Where the first line is almost black, second is red, third is green, fourth is all blue, fifth is light-greenish, and the last is a reddish-purple. Now all of these elements will be stored into a class called BallInfo which is right here:
I need to be able to spawn these balls in at the top of the screen and have them fall until they hit the bottom of the screen or a player-controlled slider that will make them disappear. When a ball hits the slider, it should be updated in the BallInfo class aforementioned. How would I go about spawning the balls in continuously and then updating the scoreboard when a ball hits the slider at the bottom of the screen?
public class BallInfo extends Actor
{
private Color ballColor; // color of the balls being summarized
private int totalCount; // number of balls of this color ever seen in the world
private int caughtCount; // number of balls of this color ever "caught"
/**
* constructor for color and stats information about a ball type.
* @param redValue the amount of red (0-255) in the RGB color to use
* @param greenValue the amount of green (0-255) in the RGB color to use
* @param blueValue the amount of blue (0-255) in the RGB color to use
*/
public BallInfo(int redValue, int greenValue, int blueValue)
{
// initially, all counts will be zero.
totalCount=0;
caughtCount=0;
// use specified color to create (redraw) image
ballColor = new Color(redValue,greenValue,blueValue);
redraw();
}
// private method to redraw the stats image.
private void redraw(Color col)
{
// build a new image for this object
GreenfootImage img = new GreenfootImage(150,12);
// fill in the background color.
img.setColor(new Color(200,200,200)); // could use Color.GRAY
img.fill();
// add an "icon" that shows the corresponding color
GreenfootImage ball = new GreenfootImage(10,10);
ball.setColor(col);
ball.fillRect(0,0, ball.getWidth(), ball.getHeight());
img.drawImage(ball, 1, 1);
// add in the count statistics for this ball color
img.setColor(new Color(0,0,0) ); // could use Color.BLACK
String stats = String.format("#:%6d", totalCount);
img.drawString(stats, 21, 11);
stats = String.format("C:%6d", caughtCount);
img.drawString(stats, 85, 11);
setImage(img); // use the image we've built up.
}
// redraw the image with the current ball calor.
private void redraw()
{
redraw(ballColor);
}
/**
* returns the color for this ball type. Might be useful to help draw a ball in the game
* @return the color for this type
*/
public Color getColor()
{
return ballColor;
}
/**
* returns the number of times a ball of this color has been added to the world
* @return the count of the number of balls of this color ever seen in the world
*/
public int getTotal()
{
return totalCount;
}
/**
* returns the number of times a ball of this color has been caught
* @return the count of the number of balls of this color ever been caught
*/
public int getCaught()
{
return caughtCount;
}
/**
* adds one to the total number of balls of this color seen in the world.
*/
public void incTotal()
{
totalCount++;
redraw();
}
/**
* adds one to the total number of ballos of this color that have been caught
*/
public void incCaught()
{
caughtCount++;
redraw();
}
