Sorry, that is my fault. Let me post the code for both classes again. You can copy/paste both into your scenario and it should run fine.
First, the SpaceWorld class:
Now, the Star class:
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 | import greenfoot.*; import java.awt.Color; public class SpaceWorld extends World { final int starCount = 100 ; public SpaceWorld() { super ( 600 , 400 , 1 ); createBackgroundImage(); createStars(); } public void createBackgroundImage() { getBackground().setColor(Color.BLACK); getBackground().fill(); } public void createStars() { for ( int i = 0 ; i < 100 ; i++) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject( new Stars(), x, y); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import greenfoot.*; import java.awt.Color; public class Star extends Actor { private int speed; public Star() { int size = Greenfoot.getRandomNumber( 4 + 1 ; GreenfootImage img = new GreenfootImage(size, size); img.setColor(Color.white); img.fillOval( 0 , 0 , size, size); setImage(img); speed = Greenfoot.getRandomNumber( 4 ) + 1 } public void act() { int x = (getX() + speed) % getWorld().getWidth(); setLocation(x, getY()); } } |