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

2013/6/29

Scrolling the image of the World

Kartoffelbrot Kartoffelbrot

2013/6/29

#
I have a scrolling world (like in Skyway), but it only scrolls, by moveing all objects. How can I move the background image of the world?
Gevater_Tod4711 Gevater_Tod4711

2013/6/29

#
You can't realy scroll the image because you can't set it's location. To make it look like the backgroud image is scrolled you need to change the starting point of the image. This coordinate point you can scroll. If you use this point which is scrolled like the objects in the world as the starting point for your image (the point from which you start drawing the image) it looks like you are scrolling the background image. You may need to add the image size to this value as long as it is not in the world. Maybe this method I used in my Infinite Scrolling World scenario can help you:
    /**
     * Creates a moving textur on the background image of the world.
     */
    protected final void createTextur() {
        int x;
        int y;
        if (totalXMovement > 0) {
            for (x = totalXMovement; x > 0; x -= textur.getWidth()) {
                ;
            }
        }
        else {
            for (x = totalXMovement; x < 0; x += textur.getWidth()) {
                ;
            }
            x -= textur.getWidth();
        }
        if (totalYMovement > 0) {
            for (y = totalYMovement; y > 0; y -= textur.getHeight()) {
                ;
            }
        }
        else {
            for (y = totalYMovement; y < 0; y += textur.getHeight()) {
                ;
            }
            y -= textur.getHeight();
        }
        getBackground().clear();
        for (int i = x; i < getWidth(); i += textur.getWidth()) {
            for (int j = y; j < getHeight(); j += textur.getHeight()) {
                getBackground().drawImage(textur, i, j);
            }
        }
    }
Kartoffelbrot Kartoffelbrot

2013/6/30

#
Thanks.
You need to login to post a reply.