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

2013/6/4

Scrolling background

welleid welleid

2013/6/4

#
Hey, i have a simple question, I have created a big picture, 800x3000. My world is a 800x900 world with 1x1 cells. I want my background to scroll from top to down, like an infinite scroll (my picture is set so it's like one infinite picture). I began to try something, and based on something i found here,
1
2
3
GreenfootImage bg = new GreenfootImage(getBackground() ); 
        getBackground().drawImage(bg, 0, + 1);  //On redessine le bg par dessus
        getBackground().drawImage(bg, 0, getHeight() + 10);  //En le faisant défiler
But it's not working as expected. I tried several other things, but none worked. Any idea ?
Solringolt Solringolt

2013/6/4

#
Maybe you ca try something by adding false to the world property super(800,900,1,false) Like that the world has no limit and you can add an image at (-1000, 0) for example So you can set your image at any position. Then you can make it move to the bottom. when the top reach the window add a new image on top of this and when the top reach the bottom of the window you remove it. You do that on and on so you get your background scrolling. If you need more help tell me and I can post an exemple.
danpost danpost

2013/6/4

#
Making the world 'unbounded' by adding the 'false' parameter to the world property does not help in scrolling the world background. Also, when using an image that is not the same size as the world, you will need an instance int field to track the scroll amount as well as an instance GreenfootImage field to hold the image to draw on the background:
1
2
3
4
5
6
7
// with instance fields (in world class)
private GreenfootImage bgImage = new GreenfootImage(/* "name of image" */);
private int scrollAmount;
// in the act method or method it calls (in world class)
scrollAmount = (scrollAmount+1)%bgImage.getHeight();
if (scrollAmount < getHeight()) getBackground().drawImage(bgImage, 0, scrollAmount-bgImage.getHeight());
getBackground().drawImage(bgImage, 0, scrollAmount-2*bgImage.getHeight());
welleid welleid

2013/6/4

#
It's okay I found a good solution, thanks anyway for your help !
MUKMUKMUK MUKMUKMUK

2015/6/14

#
Welleid can you tell me where did you get the other solution from because I don't understand what they're talking about and btw im a noob at computing and sorry if my english is bad im from japan.
You need to login to post a reply.