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

2013/5/4

Background star shift

GreenGoo GreenGoo

2013/5/4

#
I have been trying for weeks now to recreate davmac's background in Skulls in space ( http://www.greenfoot.org/scenarios/1841) and to a lesser extent GameCode's background (http://www.greenfoot.org/scenarios/3295) although it does not work as well. I can't find a way of doing it as well as davmac has, but I would really like to be able to. Thanks for any help.
danpost danpost

2013/5/5

#
Code everything as you normally would, except in the Ship class, instead of moving the ship, move all the stars in the opposite direction (multiplied by their speed factor). I just created a 'clone' of davmac's Skulls in Space scenario. Very close! Slight difference in the spawning and movement of skulls; plus adjustment in shot spawning to make it emit from the head of the ship (evidenced when turning in the original).
GreenGoo GreenGoo

2013/5/5

#
Did you mean to make 'Skulls in Space' a hyperlink?
danpost danpost

2013/5/5

#
No. I did not upload my 'clone'. It was just to signify it was the name of a scenario (just underlined text).
danpost danpost

2013/5/5

#
My Star class code follows:
import greenfoot.*;
import java.awt.Color;

public class Star extends Actor
{
    private double x, y;
    private int speed;
    
    public Star()
    {
        int size = Greenfoot.getRandomNumber(5)+1;
        GreenfootImage image = new GreenfootImage(size, size);
        image.setColor(Color.white);
        image.fillOval(0, 0, size, size);
        setImage(image);
        speed = Greenfoot.getRandomNumber(3)+1;
    }
    
    public void addedToWorld(World world)
    {
        x = getX();
        y = getY();
    }

    public void move(int rot)
    {
        x -= Math.cos(Math.toRadians(rot))*speed;
        y -= Math.sin(Math.toRadians(rot))*speed;
        x = (x+850)%800-50; // creates a wrap-around feature
        y = (y+650)%600-50; // creates a wrap-around feature
        setLocation((int)x, (int)y);
    }
}
My world was created with 'super(700, 500, 1, false);' (same size as davmac's world). The last method above is called on all stars in the world using a 'for' loop in the act method of the Rocket class as follows:
for (Object obj : getWorld().getObjects(Star.class))
{
    Star star = (Star) obj;
    star.move(getRotation());
}
Oh, and I added the stars into the world with the following in the world constructor:
for (int i=0; i<50; i++)
{
    int x = Greenfoot.getRandomNumber(getWidth()+100)-50;
    int y = Greenfoot.getRandomNumber(getHeight()+100)-50;
    addObject(new Star(), x, y);
}
GreenGoo GreenGoo

2013/5/5

#
Thanks, I'll try it out in a bit.
GreenGoo GreenGoo

2013/5/5

#
Thanks, it works very well.
You need to login to post a reply.