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

2015/4/25

Scrolling world, up to down [PLEASE help]

pandu4431 pandu4431

2015/4/25

#
hey guys, please help me with this code to make up to down scrolling. i tried many, but i cant do that with this code, i just follow some tutorial from website Please help me MASTER
public class ScrollWorld extends World 
{ 
    private static final GreenfootImage bgImage = new GreenfootImage("Background 2.jpg");
    private static final int scrollSpeed = 1;
 
    private GreenfootImage scrollingImage;
    private int scrollPosition = 0;
     
    public ScrollWorld()
    {
        super(400, 600, 1);
      
        GreenfootImage background = new GreenfootImage(400, 600);
        scrollingImage = getScrollingImage(400, 600);
        background.drawImage(scrollingImage, 0, 0);
        setBackground(background);
       
        addObject(new SpaceShip(), 200, 540);
    }

    public void act()
    {
        if(scrollSpeed > 0 && scrollPosition <= 0) {
            scrollPosition = getWidth();
        }
        if(scrollSpeed < 0 && scrollPosition >= getWidth()) {
            scrollPosition = 0;
        }
        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);
        bg.drawImage(scrollingImage, position - scrollingImage.getWidth(), 0);
    }
 
    /**
     * Returns an image with the given dimensions.
     */
    private GreenfootImage getScrollingImage(int width, int height)
    {
        GreenfootImage image = new GreenfootImage(width, height);
        for(int x = 0; x < width; x += bgImage.getWidth()) {
            for(int y = 0; y < height; y += bgImage.getHeight()) {
                image.drawImage(bgImage, x, y);
            }
        }
        return image;
    } 
}
danpost danpost

2015/4/25

#
The code given appears to be for a side-scroller. If you switch how the 'x' and y' values are being handled, you can get a vertical-scroller. The scroll speed may need to be negated to reverse the scrolling direction.
pandu4431 pandu4431

2015/4/25

#
where the method i can switch the value?, i have switch in getScrollingImage, but i didn't change anything, can you rewrite for me? please master
danpost danpost

2015/4/25

#
pandu4431 wrote...
where the method i can switch the value?, i have switch in getScrollingImage, but i didn't change anything, can you rewrite for me? please master
Its is not just one specific place. You need to scour through the entire code and rewrite it so that the scrolling is along the vertical instead of the horizontal. This would involve swapping 'x' and 'y' values and changing 'getWidth' calls to 'getHeight' calls where needed. For example, lines 40 and 41 would end up being something like the following:
bg.drawImage(scrollingImage, 0, position);
bg.drawImage(scrollingImage, 0, position - scrollingImage.getHeight());
Compare this to what you had above. Lines 24 and 26 are two lines that would need changing. BTW, the 'getScrollingImage' method does not deal specifically with the scrolling (it just creates the image that is to be scrolled). No changes should have been made to that method.
pandu4431 pandu4431

2015/4/25

#
thanks a lot master, now my games run correctly as i want thanks thanks thanks
pandu4431 pandu4431

2015/4/25

#
opps, i forget to ask one things, why the picture have blink when get new image layer??
danpost danpost

2015/4/25

#
pandu4431 wrote...
opps, i forget to ask one things, why the picture have blink when get new image layer??
I cannot rightly say what might be causing that behavior. There is nothing immediately apparent in the code given.
You need to login to post a reply.