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

2015/1/6

Scrolling background

berdie berdie

2015/1/6

#

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);
    }
}
I used this code, from another post. But it goes vertically I tried to adjust it but my background bugs, anyone know how to make it horizontal
danpost danpost

2015/1/6

#
If you (a) change all 'Width' to 'Height' and all 'Height' to 'Width' and then (b) reverse all paired coordinates, you will change it from a horizontal scroller to a vertical scroller. With some lines you may have to do both (a) and (b). For example, line 34 will end up like this;
bg.drawImage(bgImage, 0, position + picHeight);
and line 18 would end up like this:
bgBase = new GreenfootImage(getWidth(), picHeight);
berdie berdie

2015/1/7

#
It works, but at a certain time it stops, and how do i get it scrolling the other way not - but + ? can't get that working
danpost danpost

2015/1/7

#
berdie wrote...
It works, but at a certain time it stops, and how do i get it scrolling the other way not - but + ? can't get that working
Change the '-' to a '+' on line 24.
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);
    }
It is close to working,what am i missing?
danpost danpost

2015/1/7

#
danpost wrote...
< Quote Omitted > Change the '-' to a '+' on line 24.
This advice was not correct. The correct solution was to change the sign of 'scrollSpeed' on line 7 (in the first code post). It may appear to do the same thing; however, the sign of the field is used in determining scroll break points (see lines 25 and 26 of the first code post). A final thought on that is that the sign of the 'scrollSpeed' should not be needed as a factor in those lines -- if the value of 'scrollPosition' ever got too low or too high, then is was because the sign of the value of 'scrollspeed' was what it was checked to be. Line 29 should be adjusted to work both scrolling directions, something like this:
bg.drawImage(bgImage, 0, position-(int)Math.signum(scrollSpeed)*picHeight);
You need to login to post a reply.