I have found code (thanks danpost) which will cause the background to scroll. However I want it to scroll down the screen rather from right to left. I don't know what I need to adjust to make this happen. Any help would be appreciated.
public class Galaxy extends World
{
private static final String bgImageName = "space.jpg";
private static final double scrollSpeed = 2.5;
private static final int picWidth = (new GreenfootImage(bgImageName)).getWidth();
private GreenfootImage bgImage, bgBase;
private int scrollPosition = 0;
/**
* Constructor for objects of class Galaxy.
*
*/
public Galaxy()
{
// Create a new world with 10x400 cells with a cell size of 1x1 pixels.
super(1280, 720, 1);
setBackground(bgImageName);
bgImage = new GreenfootImage(getBackground());
bgBase = new GreenfootImage(picWidth, getHeight());
bgBase.drawImage(bgImage, 0, 0);
addObject(new Ship(), 600, 550);
}
public void act()
{
scrollPosition -= scrollSpeed;
while(scrollSpeed > 0 && scrollPosition < -picWidth) scrollPosition += picWidth;
while(scrollSpeed < 0 && scrollPosition > 0) scrollPosition -= picWidth;
paint(scrollPosition);
}
private void paint(int position)
{
GreenfootImage bg = getBackground();
bg.drawImage(bgBase, position, 0);
bg.drawImage(bgImage, position + picWidth, 0);
}
}
