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

2017/2/24

Scrolling World

joshuadswa joshuadswa

2017/2/24

#
Hello :) Can anyone explain to me how to use ScrollingWorld? i already read,watch and download the scenario, but i dont really understand how it work @_@ Thx :D
Super_Hippo Super_Hippo

2017/2/24

#
Is it danpost's scenario? It has a quite detailed description in the SWorld class about how to use it and there are actors and a subworld for an example. I think you should explain in detail with what exactly you have a problem. However, I am not an expert about how to use his scenario (see unanswered discussion here) :P
joshuadswa joshuadswa

2017/2/25

#
>. < i just want the camera follow the actor._.
danpost danpost

2017/2/25

#
There is a ScrollingWorld that was authored by grfottox. The one by SPower was called ScrollWorld. I currently cannot access the code for the ScrollingWorld class (which may -- or may not -- be the one you are using). Without the class code, it would be difficult to help. However, if you are willing to try a different scrolling class, there are several of them available here. Super_Hippo mentioned my SWorld class; however, that was an early attempt at scrolling. It works okay; but it, in my opinion, is not really built very well and not the easiest to use. My latest scrolling work, the Scroller class is what I would recommend to anyone wanting to create a scrolling world -- not because I did it; but, because it is quite simplistic in nature and quite versatile at the same time. Even if you cannot view the scenario, the description has an address where you can get the source code of the class. The basic code for the World subclass to follow an actor will be provided below.
danpost danpost

2017/2/25

#
Here is what is basically needed in you World subclass:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import greenfoot.*;
/**
 * CLASS: ImageScrollWorld
 *
 * DESCRIPTION: creates a limited (to scrolling background image size) center screen actor
 * following scrolling world
 */
public class ImageScrollWorld extends World
{
    public static final int WIDE = 800; // world width (viewport)
    public static final int HIGH = 600; // world height (viewport)
     
    Scroller scroller; // the object that performs the scrolling
    Actor scrollActor; // an actor to stay in view
     
    public ImageScrollWorld()
    {
        super(WIDE, HIGH, 1, false); // creates an unbounded world
        GreenfootImage bg = new GreenfootImage("background.png"); // creates an image to scroll (adjust as needed)
        int bgWide = bg.getWidth(); // scrolling image width
        int bgHigh = bg.getHeight(); // scrolling image height
        scroller = new Scroller(this, bg, bgWide, bgHigh); // creates the Scroller object
        scrollActor = new Player(); // creates the actor to maintain view on (adjust class name as needed)
        addObject(scrollActor, bgWide/2, bgHigh/2); // add actor at center of scrolling area (wherever)
        scroll(); // sets initial background image and puts main actor in view if needed
    }
     
    public void act()
    {
        scroll();
    }
     
    // attempts scrolling when actor is not in center of visible world
    private void scroll()
    {
        // determine scrolling offsets and scroll
        int dsx = scrollActor.getX()-WIDE/2; // horizontal offset from center screen
        int dsy = scrollActor.getY()-HIGH/2; // vertical offset from center screen
        scroller.scroll(dsx, dsy);
    }
}
The following can be used to allow some actor movement before scrolling begins:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// attempts scrolling when actor is outside roaming limits
private void scroll()
{
    // roaming limits of actor
    int loX = 100;
    int hiX = WIDE-100;
    int loY = 50;
    int hiY = HIGH-50;
    // determine offsets and scroll
    int dsx = 0, dsy = 0;
    if (scrollActor.getX() < loX) dsx = scrollActor.getX()-loX;
    if (scrollActor.getX() > hiX) dsx = scrollActor.getX()-hiX;
    if (scrollActor.getY() < loY) dsy = scrollActor.getY()-loY;
    if (scrollActor.getY() > hiY) dsy = scrollActor.getY()-hiY;
    scroller.scroll(dsx, dsy);
}
joshuadswa joshuadswa

2017/2/25

#
Okkk thx :) i will try to study it
You need to login to post a reply.