I am making a crossy road style game and i have no idea how to get the world to scroll when my character hits the edge. please help.
world code:
code for character:
public class MyWorld extends World
{
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
super(1000, 800, 1);
prepare();
}
public void act()
{
if (Greenfoot.getRandomNumber(100) == 0)
{
addObject(new RedCar(), getWidth() -930,1);
}
if (Greenfoot.getRandomNumber(100) == 0)
{
addObject(new GreenCar(), getWidth() -830 , 795);
}
if (Greenfoot.getRandomNumber(100) == 0)
{
addObject(new BlueCar(), getWidth() -880 , 1);
}
if (Greenfoot.getRandomNumber(100) == 0)
{
addObject(new Ambulance(), getWidth() -680 , 1);
}
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Lizard lizard = new Lizard();
addObject(lizard,-900,400);
Door door = new Door();
addObject(door,922,759);
lizard.setLocation(23,398);
}
}
public class Lizard extends Actor
{
GreenfootImage image1 = new GreenfootImage("LizardDown.png");
GreenfootImage image2 = new GreenfootImage("LizardLeft.png");
GreenfootImage image3 = new GreenfootImage("LizardBack.png");
GreenfootImage image4 = new GreenfootImage("LizardRight.png");
/**
* Act - do whatever the Lizard wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Move();
Hit();
}
public void Move()
{
if (Greenfoot.isKeyDown("down"))
{
if (Greenfoot.getKey()!=null)
{
down();
setImage(image1);
}
}
if (Greenfoot.isKeyDown("up"))
{
if (Greenfoot.getKey()!=null)
{
up();
setImage(image3);
}
}
if (Greenfoot.isKeyDown("right"))
{
if (Greenfoot.getKey()!=null)
{
right();
setImage(image4);
}
}
if (Greenfoot.isKeyDown("left"))
{
if (Greenfoot.getKey()!=null)
{
left();
setImage(image2);
}
}
}
public void down()
{
setLocation(getX(), getY() +50);
}
public void up()
{
setLocation(getX(), getY() -50);
}
public void right()
{
setLocation(getX() + 50, getY());
}
public void left()
{
setLocation(getX() - 50, getY());
}
public void Hit()
{
if ( isTouching(RedCar.class) ) {
removeTouching(Lizard.class);
if (isTouching(RedCar.class) )setLocation(23, 398);
// Greenfoot.setWorld(new Level2());
}
if ( isTouching(Ambulance.class) ) {
removeTouching(Lizard.class);
if (isTouching(Ambulance.class) )setLocation(23, 398);
// Greenfoot.setWorld(new Level2());
}
if ( isTouching(GreenCar.class) ) {
removeTouching(Lizard.class);
if (isTouching(GreenCar.class) )setLocation(23, 398);
// Greenfoot.setWorld(new Level2());
}
if ( isTouching(BlueCar.class) ) {
removeTouching(Lizard.class);
if (isTouching(BlueCar.class) )setLocation(23, 398);
// Greenfoot.setWorld(new Level2());
}
if ( isTouching(Door.class)) {
removeTouching(Door.class);
//Level2.backgroundMusic.stop();
Greenfoot.setWorld(new Level2());
}
if ( isTouching(Door2.class)) {
removeTouching(Door.class);
//Level2.backgroundMusic.stop();
Greenfoot.setWorld(new Level3());
}
if ( isTouching(Door3.class)) {
removeTouching(Door.class);
//Level2.backgroundMusic.stop();
Greenfoot.setWorld(new Level4());
}
}
}
