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

2015/2/13

Scrolling Background

Mrinal Mrinal

2015/2/13

#
Hi! I have this project where there is a scrolling background. The ladybug moves with the arrow keys and when it reaches the end of the world, the background scrolls, giving the effect that the ladybug is moving, changing the view point. My only question is , How do I limit the ladybug's movement to certain co-ordinates in the world? Please help at the earliest Thanks! :) import greenfoot.*; import java.util.List; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class World1 here. * * @author (your name) * @version (a version number or a date) */ public class World1 extends World { private Hero v; public final int NORTH = 270; public final int SOUTH = 90; public final int EAST = 0; public final int WEST = 180; /** * Constructor for objects of class World1. * */ public World1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(460, 460, 1 , false); v = new Hero(); buildMaze(background1); } public void moveBackground(int dx , int dy) { List<ScrollingActor> actors = getObjects(ScrollingActor.class); for(ScrollingActor actor : actors) { actor.makeMove(-dx , -dy); } } public void buildMaze( String maze ) { int x = -69; int y = -69; for(int p=0; p<maze.length(); p++) { if( maze.charAt( p ) == 'T' ) { addObject( new Tree(), x, y ); } if( maze.charAt( p ) == 'R' ) { addObject(new Rock(), x, y ); } if( maze.charAt( p ) == 'H' ) { addObject( v, x, y ); } if( maze.charAt( p ) == 'G' ) { addObject( new Grass(), x, y ); } x += 46; if( x>getWidth() ) { x = -69; y += 46; } } } public void act() { if(Greenfoot.isKeyDown("right") && v.getX()<=414 ) { v.setRotation(0); v.move(3); } if(Greenfoot.isKeyDown("left") && v.getX()>=46) { v.setRotation(180); v.move(3); } if(Greenfoot.isKeyDown("up") && v.getY() >=46) { v.setRotation(270); v.setLocation(v.getX() ,v. getY()-3); } if(Greenfoot.isKeyDown("down") && v.getY()<=414) { v.setRotation(90); v.setLocation(v.getX() , v.getY()+3); } if(v.getX() <= 46 && Greenfoot.isKeyDown("left") ) { v.setRotation(180); moveBackground(-3 , 0); } if(v.getX() >=414 && Greenfoot.isKeyDown("right") ) { v.setRotation(0); moveBackground( 3 , 0); } if( v.getY()<=46 && Greenfoot.isKeyDown("up") ) { v.setRotation(270); moveBackground(0 , -3); } if(v.getY()>=414 && Greenfoot.isKeyDown("down")) { v.setRotation(90); moveBackground(0 , 3); } } private String background1 = "OOGOOTOOOOROOO" + "OOROOOOTOOOOGO"+ "OOOOOOOOOOOOOO"+ "OOOROOTOOGOOOO"+ "OOOOOOOOOOOOOO"+ "OOOOGOOOTOROOO"+ "OGOOOGOOOTOOOR"+ "OOOOOOGROOTROO"+ "OOOGOHTOOTOROO"+ "OROOOGOTOOROOO"+ "OOOOOOROOOOTOG"+ "OOROOOOTOGOORO"+ "OROOOOROGOOOTO"+ "OOOTOOOROOOTGO"; }
davmac davmac

2015/2/13

#
danpost danpost

2015/2/13

#
Are you trying to just limit the location of the ladybug to an rectangle that is smaller than the world window? or are you really asking how to limit the amount of scrolling allowed?
Mrinal Mrinal

2015/2/14

#
I'm asking how to limit the amount of scrolling allowed...
danpost danpost

2015/2/14

#
To limit the amount of scrolling, you will need to track the amount of scrolling and then make sure any potential new value is within your set limits. It appears your world is created so that you start in the center of the scrolling area (you will be able to scroll the same amount in all directions before the limit is reached). If the scroll amounts are initialized to zero, then your limits will be at 92 and -92. For limiting a potential new value, I usually do something like this;
// fields or variables used
int value; // holding the current value (variable as a field value maintained by the object)
int loLimit; // the minimum value of 'value' (value can be hard-coded instead of using a variable)
int hiLimit; // the maximum value of 'value' (value can be hard-coded instead of using a variable)
int change; // the potential change in the value of 'value' (variable can be local to the method)

// adjust the value of 'change' so that 'value' does not exceed limits before applying the change
if (change < 0 && value+change < loLimit) change -= value+change-loLimit;
if (change > 0 && value+change > hiLimit) change -= value+change-hiLimit;
adjustValue(change); // use adjusted value of 'change' to perform the action with
The 'adjustValue' method is given as a sample method call using the 'change' value as a parameter. In your case, you will need two fields -- one for the horizontal scrolling offset and the other for the vertical. Both will need to be limited as with code similar to that above. This can be done in your 'moveBackground' method.
You need to login to post a reply.