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

