I am doing a maze game with 2 different levels but the game lags any idea on how to help with this?
Moveable object
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
* Write a description of class Object here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Object extends Actor
{
/**
* Act - do whatever the Object wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
GreenfootImage image = getWorld().getBackground();
if(Greenfoot.isKeyDown("right") && canMove("right",image)) {
move(1);
} else if(Greenfoot.isKeyDown("left") && canMove("left",image)) {
setRotation(180);
move(1);
setRotation(0);
} else if(Greenfoot.isKeyDown("up") && canMove("up",image)) {
setRotation(270);
move(1);
setRotation(0);
} else if(Greenfoot.isKeyDown("down") && canMove("down",image)) {
setRotation(90);
move(1);
setRotation(0);
}
nextLevel();
nextLevel2();
}
public boolean canMove(String direction, GreenfootImage background) {
try {
if(direction == "up") {
if(background.getColorAt(getX(),getY() - 2).equals(Color.BLACK) ||
background.getColorAt(getX() -1,getY() - 2).equals(Color.BLACK)
|| background.getColorAt(getX() + 1,getY() - 2).equals(Color.BLACK)) {
return false;
}
} else if(direction == "down") {
if(background.getColorAt(getX(),getY() + 2).equals(Color.BLACK)
|| background.getColorAt(getX() - 1,getY() + 2).equals(Color.BLACK)
|| background.getColorAt(getX() + 1,getY() + 2).equals(Color.BLACK)) {
return false;
}
} else if(direction == "left") {
if(background.getColorAt(getX() - 2,getY()).equals(Color.BLACK)
|| background.getColorAt(getX() - 2,getY() + 1).equals(Color.BLACK)
|| background.getColorAt(getX() - 2,getY() - 1).equals(Color.BLACK)) {
return false;
}
} else if(direction == "right") {
if(background.getColorAt(getX() + 2,getY()).equals(Color.BLACK)
|| background.getColorAt(getX() + 2,getY() - 1).equals(Color.BLACK)
|| background.getColorAt(getX() + 2,getY() + 1).equals(Color.BLACK)) {
return false;
}
}
} catch(Exception e) {
return false;
}
return true;
}
public void nextLevel()
{
if (getOneObjectAtOffset(0, 0, winner.class) != null)
{
World world2 = new level2();
Greenfoot.setWorld(world2);
}
}
public void nextLevel2()
{
if (getOneObjectAtOffset(0, 0, winner2.class) != null)
{
World world3 = new HardStage();
Greenfoot.setWorld(world3);
}
}
}

