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


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); } } |
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); } |