Hello Experts
I want to make an easy moving background and I found this super project, Polle's sidescroll demo
But I can not understand the code. As far as I understand it, the background image is the same image as the scrollingImage. The starting point of the scrollingImage is x/y (0/0). Whenever it is less 0, the scrollingImage is positioned to the width of the world and it is wandering to x = 0 again.
I can not unterstand how it comes that one can not see the remaining, not moving backgroundImage, but everytime the scrollingImage
Thanks for your help!
public class ScrollWorld extends World
{
//private static final GreenfootImage bgImage = new GreenfootImage("space1.gif");
private static final int scrollSpeed = 1;
private GreenfootImage scrollingImage;
private int scrollPosition = 0;
public ScrollWorld()
{
super(800, 400, 1);
scrollingImage = new GreenfootImage(getBackground());
//scrollingImage = getScrollingImage(800, 400);
addObject (new SpaceShip(), 350, 200);
}
public void act()
{
if(scrollPosition < 0) {
scrollPosition = getWidth();
}
scrollPosition -= scrollSpeed;
paint(scrollPosition);
}
/**
* Paint scrolling image at given position and make sure the rest of
* the background is also painted with the same image.
*/
private void paint(int position)
{
GreenfootImage bg = getBackground();
bg.drawImage(scrollingImage, position, 0);
if(position > 0) {
bg.drawImage(scrollingImage, position - scrollingImage.getWidth(), 0);
}
else {
bg.drawImage(scrollingImage, position + scrollingImage.getWidth(), 0);
}
}
}
