This site requires JavaScript, please enable it in your browser!
Greenfoot back
Smolitor
Smolitor wrote ...

2015/1/9

Infinite Side Scroller Implementation

Smolitor Smolitor

2015/1/9

#
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);
    }
}
danpost danpost

2015/1/9

#
I should be no problem to copy everything. Just change the name of the world to your world (lines 3 and 13), adjust the name of the background image filename (line 6) and correct the size of the world (line 15). The height dimenion of the world should be the same as the height of the image you are using for the background. Oh, and change line 7 to the following (the precision of the double is being lost in the implementation of the scrolling system anyway):
private static final int scrollSpeed = 2;
You can adjust the scrollSpeed to a suitable value. But, more than that will need changed if you decide to reverse the direction of scrolling by changing its value to a negative number.
Smolitor Smolitor

2015/1/10

#
Thank you so much! By the way, are you in any way affiliated the DAN-BALL website? Your picture reminds me of their art style, and the number of experiments/code projects makes me wonder.
danpost danpost

2015/1/10

#
Smolitor wrote...
By the way, are you in any way affiliated the DAN-BALL website?
Never heard of it.
You need to login to post a reply.