Hi please help me. How do i make Lara (person), go to a new level (world) with a different background and a different image for lara. This is her code as of now
public int counter = 0;
public void act()
{
if (canSee(Monuments.class))
{
eat(Monuments.class);
Greenfoot.playSound("slurp.wav");
counter++;
if(counter == 3 )
{
newLevel();
}
}
if (Greenfoot.isKeyDown("right"))
{
move (4);
}
if (Greenfoot.isKeyDown("left"))
{
move (-4);
}
if (canSee(Bomb.class))
{
eat(Bomb.class);
Greenfoot.playSound("au.wav");
}
}
public void newLevel()
{
World world;
world = getWorld();
int x = (int)(100+Math.random()*350);
int y = 80;
world.addObject(new Monuments(x,y) , x,y);
x = (int)(100+Math.random()*350);
world.addObject(new Monuments(x,y) , x,y);
x = (int)(100+Math.random()*350);
world.addObject(new Monuments(x,y) , x,y);
}
If you are only going to have two levels, you can just put the code in the 'newLevel' method using the 'world.setBackground(String imageFilename)' and 'setImage(String imageFilename)' methods to make the changes. If you are adding additional levels, best would be to put the filenames in arrays and add an instance int field to track the level number and determine what index in the arrays to get the names from (still, the code for incrementing the level number and setting the images would go in the 'newLevel' method).
I have three levels. I used this code for Lara and Iv'e managed to make her go to the next level but i cant make her move to the third.
public int counter = 0;
public void act()
{
if (canSee(Monuments.class))
{
eat(Monuments.class);
Greenfoot.playSound("slurp.wav");
counter++;
if(counter == 3 )
{
newLevel();
}
}
if (Greenfoot.isKeyDown("right"))
{
move (4);
}
if (Greenfoot.isKeyDown("left"))
{
move (-4);
}
if (canSee(Bomb.class))
{
eat(Bomb.class);
Greenfoot.playSound("au.wav");
}
}
public void newLevel()
{
World world;
world = getWorld();
Greenfoot.setWorld(new Asia());
}
public void newLeve2()
{
World world;
world = getWorld();
Greenfoot.setWorld(new Europe());
}
Changing thing up, eh? Before, it appeared all levels would be using the same world instance; now, it appears you are using different worlds (there were no 'setWorld' methods used in your first post). This changes things drastically. Your 'newLevel' method will need to determine what world is currently active to know which world to proceed to. This is done by using the 'instanceof' operator on the world object. For example:
if (getworld() instanceof Asia) Greenfoot.setWorld(new Europe());
You will need a similar line for each possible level change.
2014/2/11
2014/2/11
2014/2/11
2014/2/11
2014/2/11
2014/2/12