I have made a walking man animation. To make the animation more realistic i want to make the my background move backwards. Can anyone help please.


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 | import greenfoot.*; public class Background extends World { private static final String bgImageName = "brick.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 ; public Background() { super ( 800 , 400 , 1 ); setBackground(bgImageName); bgImage = new GreenfootImage(getBackground()); bgBase = new GreenfootImage(picWidth, getHeight()); bgBase.drawImage(bgImage, 0 , 0 ); } 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 ); } } |
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 | import greenfoot.*; public class Background extends World { private static final String bgImageName = "brick.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 ; public Background() { super ( 800 , 400 , 1 ); setBackground(bgImageName); bgImage = new GreenfootImage(getBackground()); bgBase = new GreenfootImage(picWidth, getHeight()); bgBase.drawImage(bgImage, 0 , 0 ); } 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 ); } } |
1 | private static final int picHeight = ( new GreenfootImage(bgImageName)).getHeight(); |
1 | bgBase = new GreenfootImage(getWidht(), picHeight); |