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

2018/11/10

Moving Backgrounds - Question

wueschn wueschn

2018/11/10

#
Hello Experts I want to make an easy moving background and I found this super project, Polle's sidescroll demo But I can not understand the code. As far as I understand it, the background image is the same image as the scrollingImage. The starting point of the scrollingImage is x/y (0/0). Whenever it is less 0, the scrollingImage is positioned to the width of the world and it is wandering to x = 0 again. I can not unterstand how it comes that one can not see the remaining, not moving backgroundImage, but everytime the scrollingImage
public class ScrollWorld extends World 
{ 
     //private static final GreenfootImage bgImage = new GreenfootImage("space1.gif"); 
     private static final int scrollSpeed = 1; 
     
     private GreenfootImage scrollingImage; 
     private int scrollPosition = 0; 
     
     public ScrollWorld() 
     { 
         super(800, 400, 1); 
         scrollingImage = new GreenfootImage(getBackground());
         //scrollingImage = getScrollingImage(800, 400);
         
         addObject (new SpaceShip(), 350, 200);
     } 

     public void act() 
     { 
         if(scrollPosition < 0) { 
             scrollPosition = getWidth(); 
         }
         scrollPosition -= scrollSpeed; 
         paint(scrollPosition); 
     }
     
     /** 
      * Paint scrolling image at given position and make sure the rest of 
      * the background is also painted with the same image. 
      */ 
     private void paint(int position) 
     { 
         GreenfootImage bg = getBackground(); 
         bg.drawImage(scrollingImage, position, 0); 
         if(position > 0) { 
             bg.drawImage(scrollingImage, position - scrollingImage.getWidth(), 0); 
         } 
         else { 
             bg.drawImage(scrollingImage, position + scrollingImage.getWidth(), 0); 
         } 
     } 
}
Thanks for your help!
danpost danpost

2018/11/10

#
wueschn wrote...
Hello Experts I want to make an easy moving background and I found this super project, Polle's sidescroll demo But I can not understand the code. As far as I understand it, the background image is the same image as the scrollingImage. The starting point of the scrollingImage is x/y (0/0). Whenever it is less 0, the scrollingImage is positioned to the width of the world and it is wandering to x = 0 again. I can not unterstand how it comes that one can not see the remaining, not moving backgroundImage, but everytime the scrollingImage << Code Omitted >>!
That particular scrolling class was programmed such that the image set as the default AND initially shown in the world (cropped to world size) is the scrolling image. If that is not what you want, then maybe you should try a different scrolling class. You may want to check out my Scrolling Tutorial scenario.
wueschn wueschn

2018/11/17

#
Thanks danpost It is perfect for my purposes, but I could not understand the code completely. The scrolling image is the background AND the image above. So for the first time the scrolling image is said to be position 0. The next time it is less than 0 and is positioned to the area size. Then it is wandering to 0 again. So there must be a situation when you see it wandering from right to left and seeing that the background image below is resting. But from beginning on it works perfekt and everything is wandering perfect without seeing something resting in the background ..... That is perfect, but it is not clear to me why it is so perfect ... Thanks again wueschn
danpost danpost

2018/11/17

#
wueschn wrote...
there must be a situation when you see it wandering from right to left and seeing that the background image below is resting. But from beginning on it works perfekt and everything is wandering perfect without seeing something resting in the background ..... That is perfect, but it is not clear to me why it is so perfect ...
I am not sure what you mean by "image below is resting". If you want clarification on some point, you will have to be more specific, show all codes used for scrolling (other than the Scroller class itself (which should not be modified) and provide sizes of both your world and the scrolling background image.
You need to login to post a reply.