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

2013/6/4

Scrolling Background in Actor?

Solringolt Solringolt

2013/6/4

#
Hi everyone, I would like to have a background that scrolls horizontally just for design, no interaction is needed. The backround image is bigger than the window. The big problem I have is that I would like to change the background that scrolls without making a new world just on a condition. Is that possible? If not I was thinking to make an object in Actor that has the same properties.
danpost danpost

2013/6/4

#
All you need to do is add an instance GreenfootImage field to the world class to hold the current background image. Since I do not know the direction or speed of the scrolling, I will also include an instance int to hold the scroll speed.
// with instance fields (in world class)
private GreenfootImage bgImage = new GreenfootImage(/* "name of image" */);
private int scrollAmount;
private int scrollSpeed = 1; // sign is direction, number is speed
// in the act method or method it calls (in world class)
scrollAmount = (scrollAmount+scrollSpeed)%bgImage.getWidth();
if (scrollAmount < getWidth()) getBackground().drawImage(bgImage, scrollAmount-bgImage.getWidth(), 0);
getBackground().drawImage(bgImage, scrollAmount-2*bgImage.getWidth(), 0);
To change the background image, just set 'bgImage' to a new image.
You need to login to post a reply.