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

2016/4/20

Setting up a vertical background scroll

wslade wslade

2016/4/20

#
I have found code (thanks danpost) which will cause the background to scroll. However I want it to scroll down the screen rather from right to left. I don't know what I need to adjust to make this happen. Any help would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Galaxy extends World
{
    private static final String bgImageName = "space.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;
    /**
     * Constructor for objects of class Galaxy.
     *
     */
 
    public Galaxy()
 
    {
 
        // Create a new world with 10x400 cells with a cell size of 1x1 pixels.
        super(1280, 720, 1);
        setBackground(bgImageName);
        bgImage = new GreenfootImage(getBackground());
        bgBase = new GreenfootImage(picWidth, getHeight());
        bgBase.drawImage(bgImage, 0, 0);
        addObject(new Ship(), 600, 550);
 
    }
 
    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

2016/4/20

#
wslade wrote...
I have found code (thanks danpost) which will cause the background to scroll. However I want it to scroll down the screen rather from right to left. I don't know what I need to adjust to make this happen. Any help would be appreciated. < Code Omitted >
Where did you find this code? It looks like it came from something ages ago. You may want to look at my (or use) my Bee Quick (ImgScroll Demo) scenario. The Scroll class is fully re-usable (meaning no changes to the class need to be made for it to work in other projects).
wslade wslade

2016/4/21

#
Okay, thanks! I had done a google search and it was from a previous discussion. I guess it was from some time ago.
danpost danpost

2016/4/21

#
wslade wrote...
Okay, thanks! I had done a google search and it was from a previous discussion. I guess it was from some time ago.
Yeah. I found it. it was almost 3 years ago.
You need to login to post a reply.