No. When using the background of the world, you can have one layer. I am guessing, now, that you want some stars to move faster than others. It would probably be better to use an actor for the stars, let then get a randomly chosen speed and let them re-position themselves when reaching the edge of the world. The only thing you need to do in the world is color the background black and create the star objects, placing them randomly in the world.
OK, the above is right for the Star class.
Now, in the world, you will call 'createStars();' from the constructor; and its code is
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()); } } |
1 2 3 4 5 6 7 8 9 | private void createStars() { for ( int i = 0 ; i < 100 ; i++) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject( new Star(), x, y); } } |