Hi!
I have another question,
How I can Scroll the background of my game?
This is the entire : http://prnt.sc/dh4jep
And this is how in game look the first part: http://prnt.sc/dh4jhn
So that's it.
Thanks for the help!


1 2 3 4 5 | if (segundoMapa){ if (getY() > 459 && Greenfoot.isKeyDown( "right" )){ ((Episodio_1_2)getWorld()).scroll(); } } |
1 | public void setLocation( int x, int y) { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // instance reference field in Episodio_1_2 class Actor scrollActor = null ; // assigned an actor in the constructor of the class // in act scroll(); // the scroll method private void scroll() { if (scrollActor == null || scrollActor.getWorld() != this ) return ; 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 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | private void moveLeft() { if (getOneIntersectingObject(Wall. class ) != null || getOneIntersectingObject(WallHorizontal. class ) != null ){ setLocation(getX() + 15 , getY()); } else { setLocation(getX() - speed, getY()); } } private void moveRight() { if (getOneIntersectingObject(Wall. class ) != null || getOneIntersectingObject(WallHorizontal. class ) != null ){ setLocation(getX() - 15 , getY()); } else { setLocation(getX() + speed, getY()); } } private void moveDown() { if (getOneIntersectingObject(Wall. class ) != null || getOneIntersectingObject(WallHorizontal. class ) != null ){ setLocation(getX(), getY() - 15 ); } else { setLocation(getX(), getY() + speed); } } private void moveUp() { if (getOneIntersectingObject(Wall. class ) != null || getOneIntersectingObject(WallHorizontal. class ) != null ){ setLocation(getX(), getY() + 15 ); } else { setLocation(getX(), getY() - speed); } } |
1 2 3 4 | int dsx = 0 ; int dsy = 0 ; if (getX() < bLineX) dsx = getX()-bLineX; if (getX() > aLineX) dsx = getX()-aLineX; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | int speed = 1 ; // either pre-defined or set here int dx = 0 ; int dy = 0 ; if (Greenfoot.isKeyDown( "right" )) dx++; if (Greenfoot.isKeyDown( "left" )) dx--; if (Greenfoot.isKeyDown( "down" )) dy++; if (Greenfoot.isKeyDown( "up" )) dy--; for ( int i= 0 ; i<speed; i++) { setLocation(getX()+dx, getY()+dy); if (isTouching(Wall. class )) { setLocation(getX()-dx, getY()-dy); break ; } } |
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 42 43 | public class Episodio_1_2 extends World { private Scroller scroller; GreenfootSound bgMusic = new GreenfootSound( "bgm/run.mp3" ); Actor scrollActor = null ; public Episodio_1_2() { super ( 800 , 622 , 1 ); scrollActor = new Kaze( "right" ); addObject( new WallHorizontal( 100 , 50 ), 41 , 416 ); addObject( new WallHorizontal( 100 , 50 ), 42 , 302 ); addObject( new WallHorizontal( 200 , 50 ), 132 , 464 ); addObject( new WallHorizontal(), 271 , 163 ); addObject( new WallHorizontal(), 478 , 556 ); addObject( new Wall(), 72 , 82 ); addObject( new Wall(), 119 , 38 ); addObject( new Wall( 130 , 400 ), 456 , 221 ); addObject( new Wall( 50 , 100 ), 210 , 539 ); scroller = new Scroller( this , new GreenfootImage( "map_exterior.PNG" ), 4311 , 622 ); bgMusic.playLoop(); } public void act(){ scroll(); } public void scroll() { if (scrollActor == null || scrollActor.getWorld() != this ) return ; int dsx = 0 ; int dsy = 0 ; if (getX() < bLineX) dsx = getX()-bLineX; if (getX() > aLineX) dsx = getX()-aLineX; scroller.scroll(dsx, dsy); } } |
1 | super ( 800 , 622 , 1 ); |
1 | super ( 800 , 622 , 1 , false ); |
1 | super ( 800 , 622 , 1 ); |
1 | super ( 800 , 622 , 1 , false ); |