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.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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 ); } } |