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

2012/4/23

What is the general code for a sidescrolling game?

MakerOfGames MakerOfGames

2012/4/23

#
.
danpost danpost

2012/4/23

#
import greenfoot.*;

public class ScrollWorld extends World
{
    int baseX = 0;
    // 'bgi' stands for BackGroundImage
    GreenfootImage bgi = new GreenfootImage("scrolling_background.png"); 
    int bgiWidth = bgi.getWidth();

   public ScrollWorld()
    {    
        super(800, 400, 1);
        update();
    }
    
    public void act()
    {
        baseX = (baseX + bgiWidth - 1) % bgiWidth;
        update();
    }
    
    private void update()
    {
        if (baseX != 0) getBackground().drawImage(bgi, baseX - bgiWidth, 0);
        if (baseX < getWidth()) getBackground().drawImage(bgi, baseX, 0);
    }
}
This is from my Sample Scroller scenario which I had up for a little while. It did not get much attention, so I took it off. However, it is the basics of what you need. My image was 1200 x 400, but as long as the image is wider than the world width (and at least as high), it should work.
JayWood860 JayWood860

2012/4/24

#
for vertical scrolling, would you replace all X's with Y's and all Width's with Height's?
You need to login to post a reply.