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.
You need to login to post a reply.