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

2013/7/2

Moving Background

RahulUK RahulUK

2013/7/2

#
How do I get my 'space' background movine forward.?This will make my rocket look like it is moving forwards.
umar17a umar17a

2013/7/2

#
Well you put the computer monitor on wheels and it should scrol along nicely
RahulUK RahulUK

2013/7/2

#
Great idea!!!
umar17a umar17a

2013/7/2

#
not
Gevater_Tod4711 Gevater_Tod4711

2013/7/2

#
You first need to have a counter in your world that counts down every act (how fast it counts is up to you). This value can be used as the starting point of your background image. Then you need to redraw the background image every act like this:
//in your world class
private int imageCount = 0;

private GreenfootImage bgImage = new GreenfootImage("imageName.png");

public void act() {
    imageCount -= 5; //(or any other value; small -> slow moving, big -> fast movement)
    drawBackgroundImage();
}

public void drawBackgroundImage() {
    if (imageCount > bgImage.getWidth()) {
        imageCount += bgImage.getWidth();
    }
    int temp = imageCount;
    getBackground().drawImage(bgImage, temp, 0);
    getBackground().drawImage(bgImage, temp + bgImage.getWidth(), 0);
}
That will make it look like your background moves.
danpost danpost

2013/7/2

#
@Gevater_Tod4711, how will the imageCount ever be greater than the width of the image if you are never increasing its value? I think line 12 should be:
if (imageCount < -bgImage.getWidth()) {
Gevater_Tod4711 Gevater_Tod4711

2013/7/2

#
Oh yes that would make more sence. Thanks danpost.
RahulUK RahulUK

2013/7/4

#
Thanks guys. i will try that
You need to login to post a reply.