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

2012/6/23

How do you make a game with scrolling

steved steved

2012/6/23

#
How do you make a world scroll, for example when an actor get somewhat near the edge of a world it scrolls that way. Is there a way to make a large world and only display part of it at once? I've seen alot of game like this and if it's not to hard I would like to try and make my game have scrolling.
danpost danpost

2012/6/23

#
I think what you mean, is make a large image for a standard size world (or multiple images to string together). You will need to first clear the current background image, and, if there is any transparencies in your large image, fill it with Color.white. Then, if you have one big image, you can drawImage it onto the background image. You will need to keep track of the base x value for the image (the x value of the large image that will be displayed at the left edge of the screen). If 'baseX' held that value, then you would 'getBackground().drawImage(largeImage, -baseX, 0);'. You would have to control the range of baseX (that it does not go past 'largeImage's width - 'background's width). If you want sample code, I can upload a demo for you. It uses 3 images 400 x 400 in a world of 600 x 400 and repeats the order of images continuously.
steved steved

2012/6/24

#
Does this work like i'ts zooming in on a certain section of the world? If so does it make the objects larger and let you veiw a certain section of a normal zoomed in?
danpost danpost

2012/6/24

#
There is no zooming, here. Just taking an over-sized image and showing the section that the actor is in.
MatheMagician MatheMagician

2012/6/24

#
Danpost's method is essentially what I used for my scrolling world demo. Along with his method though, you will have to add code to your main actor that tells it to move all the other objects back instead of it going forward. Also, when you create the world, you will have to say
super(600,400,false);
instead of
super(600,400);
This code allows the objects in the world to move smoothly out of the screen as opposed to getting stuck on the edge like usual.
steved steved

2012/6/24

#
I'm still not entirely sure what you guys mean. You are saying this takes an over sized image of where your actor is, does that mean that it shows one part of the world at once and you can move where that part is? I'm not sure what you mean by taking an oversized image of it. Does it mean you are making all of the images larger? Also where can I find the example scenarios?
K_O_P K_O_P

2012/6/24

#
Take a look at this scenario: http://www.greenfoot.org/scenarios/3040 I always recommend this, when s.o. is aking for scrolling! :) It is a good basic for a scrolling simulation/game.
danpost danpost

2012/6/24

#
Yes, using the bounds parameter in the World constructor, and setting it to 'false', would help to allow other actors to smoothly enter and exit the 'viewport'. However, I would suggest that the world move all the actors and scroll the background. It can keep tabs on where the actor is, and if out of bounds (too close to the left or right edge of the world), get the distance past the limit and move all the actors back that amount (including the player) and scroll the background that same amount.
danpost danpost

2012/6/24

#
steved wrote...
does that mean that it shows one part of the world at once and you can move where that part is?
Yes, that is exactly what we mean. You can have a world of 400 x 400 and an image of 1600 x 400 and show any 400 x 400 part of the image as the background for the world.
steved wrote...
Does it mean you are making all of the images larger?
There is no scaling of images. Nothing is being enlarged.
MatheMagician MatheMagician

2012/6/24

#
I was referring to my scenario at http://www.greenfoot.org/scenarios/5279. However, Busch2207's and my scenario implement the code I explained above, not the scrolling when the actor reaches a certain point. You would have to replace the Man code's setLocation() override method in my scenario with:
public void scroll(int x, int y)
{
        int x2 = getX()-x;
        int y2 = getY()-y;
        
        List<Others> everyone = getWorld().getObjects(Others.class);
        for(int g = 0;g < everyone.size();g++)
            {
                Actor one = everyone.get(g);
                int first = one.getRotation();
                one.setRotation(getRotation()-90);
                one.setLocation(one.getX()+x2,one.getY()+y2);
                one.setRotation(first);
            }
        if(everyone.size() > 0)
            ((Scroller) getWorld()).setThing(x2, y2);
}
and the code in boy with
if(getX() <= minimumX || getX() >= maximumX ||getY() <= minimumY || getY() >= maximumY)
                scroll();
else
                setLocation(whereEverX, whereEverY);
I imagine you would do something similar with Bush's Scrollworld.
steved steved

2012/6/24

#
So basicly you guys are saying I can make a world that objects can move off of in a way that you cannot see them but they are still there. So if you are moving forward all of the objects can move backwards.
danpost danpost

2012/6/24

#
Yes, you can move right and have object go off the left side of the screen; then, go left across the screen, and then watch as the objects return into view just as they were before.
angelomoloboco angelomoloboco

2014/9/25

#
May i have a demo sample featuring the codes above? I am looking to have the same feature on the game that I am creating.
Super_Hippo Super_Hippo

2014/9/25

#
http://www.greenfoot.org/search?query=scroll+world&commit= There are some scenarios which you can download and see how it was done.
You need to login to post a reply.