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

2012/1/6

STARS

theDoctor theDoctor

2012/1/6

#
/** * Creates stars to be displayed on the background image. * The parameter shows the number of stars on the background image. */ public void createStars(int number) { for(int i = 0; i < number; i++) { GreenfootImage background = getBackground(); int x = Greenfoot.getRandomNumber(0 - 500); int y = Greenfoot.getRandomNumber(0 - 500); GreenfootImage image = new GreenfootImage(width, height); image.setColor(Color.WHITE); image.fillOval(2, 2, width, height); setImage(image); addObject(new Star(), x, y); } } I AM TRYING TO PAINT STARS ONTO THE BACKGROUND OF MY ASTEROIDS PROGRAM, BUT WHEN I COMPILE, THIS MESSAGE KEEPS POPPING UP WHILE HIGHLIGHTING "width" FROM THE "GreenfootImage image = new GreenfootImage (width, height); IN RED: "width is not public in greenfoot.World; cannot be accessed from outside the package". I NEED HELP!
davmac davmac

2012/1/6

#
STOP SHOUTING! (Turn off your caps-lock key. Write with lower-case letters!).
davmac davmac

2012/1/6

#
Now, to help: the error is basically that you are not actually specifying values for the width and height of the image you are creating. The line: GreenfootImage image = new GreenfootImage(width, height); ... is basically correct but what is "width"? What is "height"? You need to specify actual values for these parameters. Eg: GreenfootImage image = new GreenfootImage(10, 10); (Assuming you want the image to be 10 pixels wide and 10 high).
DonaldDuck DonaldDuck

2012/1/7

#
I assume this method is in the world class and you wand to draw stars onto the background. You have a couple issues though. Your greenfoot getrandomnumber commands can't have a range (0 to 500) and writing 0-500 will get numbers between 0 and -500 (if it even deals with negatives) image should be a new GreenfootImage(getWidth(), getHeight()); image.fillOval should be image.fillOval(2,2,x,y) setImage should be setBackground(image) And if you're already painting a star onto the world background, I don't understand why you need to also add a star object...
theDoctor theDoctor

2012/1/9

#
Thanks guys this helps a lot. One thing though becuase I'm fairly new to this, what should I put as my values for my getRandomNumber commands? I'm taking a Beginning Programming class in high school, and we've been using Greenfoot for pretty much everything. It's only the second semester for me working with programming, so I don't know that much. Again, thanks for the help. I WON'T SHOUT ANYMORE IF THAT MAKES YOU FEEL BETTER! But I need to know what I should put for my getRandomNumber commands if I want the stars to be painted randomly on the background.
davmac davmac

2012/1/9

#
int x = Greenfoot.getRandomNumber(500); The above will set x to a value between 0 and 499 inclusive. Pick a y-value as well, then just draw an oval on the world's background image.
theDoctor theDoctor

2012/1/10

#
Thanks so much davmac. You've really helped here!
You need to login to post a reply.