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

2015/1/7

Help with scrolling background

berdie berdie

2015/1/7

#
private static final String bgImageName = "watergoed.jpg";
private static final double scrollSpeed = 2.5;
private static final int picHeight = (new GreenfootImage(bgImageName)).getHeight();
 
private GreenfootImage bgImage, bgBase;
private int scrollPosition = 0;
public game3()
{    
    super(900, 900, 1); 
      setBackground(bgImageName);
    bgImage = new GreenfootImage(getBackground());
    bgBase = new GreenfootImage(getWidth(), picHeight);
    bgBase.drawImage(bgImage, 0, 0);
    prepare();
}
 
public void act()
{
 
    scrollPosition += scrollSpeed;
    while(scrollSpeed > 0 && scrollPosition > -picHeight) scrollPosition -= picHeight;
    while(scrollSpeed < 0 && scrollPosition < 0) scrollPosition += picHeight;
    paint(scrollPosition);
}
private void paint(int position)
{
    GreenfootImage bg = getBackground();
    bg.drawImage(bgBase,0,position);
    bg.drawImage(bgImage,0 , position + picHeight);
}
The code doesn't work it does go Vertical, but it won't overlap right so it looks very weird. The scrolling image falls from the top just on the other one.
danpost danpost

2015/1/7

#
I think I lead you wrong in the other discussion. Try this;
import greenfoot.*;

public class game3 extends World
{
    private static final String bgImageName = "watergoed.jpg";
    private static final int scrollSpeed = 2;
    private static final int picHeight = (new GreenfootImage(bgImageName)).getHeight();
      
    private GreenfootImage bgImage, bgBase;
    private int scrollPosition = 0;
    
    public game3()
    {    
        super(900, 900, 1); 
        setBackground(bgImageName);
        bgImage = new GreenfootImage(getBackground());
        bgBase = new GreenfootImage(getWidth(), picHeight);
        bgBase.drawImage(bgImage, 0, 0);
        // prepare();
    }
      
    public void act()
    {
        scrollPosition += scrollSpeed;
        while(scrollSpeed > 0 && scrollPosition > picHeight) scrollPosition -= picHeight;
        while(scrollSpeed < 0 && scrollPosition < 0) scrollPosition += picHeight;
        paint(scrollPosition);
    }
    
    private void paint(int position)
    {
        GreenfootImage bg = getBackground();
        bg.drawImage(bgBase,0, position);
        bg.drawImage(bgImage,0 , position-picHeight);
    }
}
If it scrolls the wrong way, just change the sign of the 'scrollSpeed' on line 6.
berdie berdie

2015/1/7

#
Great I already fixed that aswell yea, but the problem I have is that sometimes 1 image overlaps another. i don't know how to fix this. So it looks like the image is falling down on the world instead of scrolling
danpost danpost

2015/1/7

#
Copy/paste what I just posted and see if it does the same. It might be the sign on the last line.
berdie berdie

2015/1/7

#
danpost wrote...
Copy/paste what I just posted and see if it does the same. It might be the sign on the last line.
Thanks for your input i already fixed it. My picture wasn't the same size as the world ( U know i can throw myself out the window right know).
You need to login to post a reply.