I am currently working on a school final project (an infinitely generated platformer that is similar in art to Mario), and one of my main problems is that I need to effectively loop an image in the background (meaning the image gradually moves across the background and will seamlessly show the image as my Definitely-Not-Mario runs across the screen. I found a post by user danpost that seems to be what I am looking for, there is even something that seems to be the exactly what I am talking about (the code shown below), I just can't figure out how to implement it. any help about what bits need to be copy pasted where would be much appreciated. Thanks in advance!
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);
}
}
