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

2016/7/20

Scrolling Background in y-Achsis Direction

Julian109998 Julian109998

2016/7/20

#
Hi guys, I'm still programming something for School, but now i tried a moving Background. I looked up different discussion about moving Backgrounds but Nnne of the shown scenarios seems to work. So can you guys help me to create a Background, that is scrolling from the top of down? I would be really thankful for an answer, Julian PS: I'm using the normal spacebackground, taht is given by Greenfoot (space.jpg)
danpost danpost

2016/7/20

#
The main thing is that you save the current background image from within the world constructor (not using the filename) and have a field to track the scrolled amount. Then, it is just a matter of drawing that saved background image twice -- once at the offset amount and once one world height away to fill the rest of the world background. Try it out, and if you have problems with the code, show what you tried and we will be glad to help.
Julian109998 Julian109998

2016/7/23

#
Thanks for the Reply, I tried different codes I found in this forum, for example the one you send to @Kaiwalya (http://www.greenfoot.org/topics/4238/0) it doesn't Show any mistakes but it simply won't work . I tried different Things also but nothing at all helps.
danpost danpost

2016/7/23

#
Julian109998 wrote...
Thanks for the Reply, I tried different codes I found in this forum, for example the one you send to @Kaiwalya (http://www.greenfoot.org/topics/4238/0) it doesn't Show any mistakes but it simply won't work . I tried different Things also but nothing at all helps.
For a continuous scroller, you just need to put the following in the world act method:
int scrollAmt = 2; // cells to scroll per act cycle (adjust as needed)
GreenfootImage bg = new GreenfootImage(getBackground());
getBackground().drawImage(bg, 0, scrollAmt); // scroll image down
getBackground().drawImage(bg, 0, scrollAmt-getHeight()); // fill area left above
Notice that line 2 creates a new image which reflects the current background image. Doing this breaks any links between the drawn image and the current background image. If you instead had used this:
GreenfootImage bg = getBackground();
(which has 'bg' referring to the current background image) then 'bg' on line 4 would represent the background image with the image drawn at the scrolled amount, which is different at the bottom edge. This would then cause the top edge to start "running" after drawing the image to fill the upper area not drawn on by line 3 (because you would be drawing the already modified background image onto itself).
danpost danpost

2016/7/23

#
danpost wrote...
For a continuous scroller, you just need to put the following in the world act method: < Code Omitted >
To scroll the actors as well, add the following to the code given:
for (Object obj : getObjects(null))
{
    Actor actor = (Actor)obj;
    actor.setLocation(actor.getX(), actor.getY()+scrollAmt);
}
If you have any Actor types that should not scroll (text or gui objects, for example), add the following to those classes:
public void setLocation(int x, int y) {}
This will prevent them from being relocated during scrolling.
Julian109998 Julian109998

2016/7/24

#
Thank you, it works perfectly :)
You need to login to post a reply.