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

2013/7/15

How can i make a scrolling background?

1
2
AreebRaza AreebRaza

2016/6/26

#
i did this and it worked but i want it to go from bottom to top
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)).getHeight();
 
    private GreenfootImage bgImage, bgBase;
    private int scrollPosition = 0;
     
    public Background()
    {    
        super(800, 400, 1);
        setBackground(bgImageName);
        bgImage = new GreenfootImage(getBackground());
        bgBase = new GreenfootImage(getWidth(), picWidth);
        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, 0, position);
        bg.drawImage(bgImage, 0, position + picWidth);
    }
}
danpost danpost

2016/6/27

#
AreebRaza wrote...
i did this and it worked but i want it to go from bottom to top < Code Omitted >
You probably just need to negate the sign of 'position' on lines 33 and 34.
You need to login to post a reply.
1
2