I have a puzzle game where a character has to get a key, open a door, and touch a treasure chest to go onto the next level. I have an int level that i use to keep track of what level I'm on and this also decides what level to go to
However, when it goes to the next level int lvl resets back to one. How can i have it increase each time I go onto a new level?
In case you need it i will post all of the code from my "Main Character" class:
Thanks in advance
public void nextLvl() { if( canSee(TreasureChest.class)) { if(lvl == 1) { Greenfoot.stop(); Greenfoot.setWorld(new Tutorial2()); } else if(lvl == 2) { Greenfoot.stop(); Greenfoot.setWorld(new Tutorial3()); } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MainChar here. * * @author (your name) * @version (a version number or a date) */ public class MainChar extends Objects { public int Keys = 0; public String facing = "down"; public int lvl = 1; /** * Act - do whatever the MainChar wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkRedDoor(); move(4); nextLvl(); checkReset(); } public void nextLvl() { if( canSee(TreasureChest.class)) { if(lvl == 1) { Greenfoot.stop(); Greenfoot.setWorld(new Tutorial2()); } else if(lvl == 2) { Greenfoot.stop(); Greenfoot.setWorld(new Tutorial3()); } } } public void move(int moveAmt) { int dy = getY(); int dx = getX(); if (Greenfoot.isKeyDown("up")) { setLocation(dx, dy-3); for(int i = 0; i < moveAmt; i++) { if (getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallRotated.class) != null || getOneIntersectingObject(WallRed.class) != null ) setLocation(getX() , getY()+1); facing = "up";} } if (Greenfoot.isKeyDown("down")) { setLocation(dx, dy+3); setImage(new GreenfootImage("MainCharFront.png")); for(int i = 0; i < moveAmt; i++) { if (getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallRotated.class) != null || getOneIntersectingObject(WallRed.class) != null ) setLocation(getX() , getY()-1); facing = "down"; } } if (Greenfoot.isKeyDown("left")) { setLocation(dx-3, dy); for(int i = 0; i < moveAmt; i++) { if (getOneIntersectingObject(WallRotated.class) != null || getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallRed.class)!= null ) setLocation(getX()+ 1 , getY()); facing = "left"; } } if (Greenfoot.isKeyDown("right")) { setLocation(dx+3, dy); for(int i = 0; i < moveAmt; i++) { if (getOneIntersectingObject(WallRotated.class) != null || getOneIntersectingObject(Wall.class) !=null) setLocation(getX()-1 , getY()); facing = "right"; } } if(canSee(Key.class)) { eat(Key.class); Keys++; } } public void checkRedDoor() { if ("x".equals(Greenfoot.getKey()) && Keys>0) { Key thrownKey = new Key(); getWorld().addObject(thrownKey,getX(), getY()-10); if(facing.equals("up")) { thrownKey.setLocation(getX()+10, getY()-30); for(int x = 10; x > 0; x-- ) { thrownKey.setLocation(thrownKey.getX(), thrownKey.getY()-1); } Keys--; } else if (facing.equals("left")){ thrownKey.setLocation(getX()-30, getY()); for(int x = 10; x > 0; x-- ) { thrownKey.setLocation(thrownKey.getX()-1, thrownKey.getY()); } Keys--; } else if(facing.equals("down")) { thrownKey.setLocation(getX()+10, getY()+40); for(int x = 10; x > 0; x--) { thrownKey.setLocation(thrownKey.getX(), thrownKey.getY()+1); } Keys--; } else if(facing.equals("right")) { thrownKey.setLocation(getX()+30, getY()); for(int x = 10; x > 0; x--) { thrownKey.setLocation(thrownKey.getX()+1, thrownKey.getY()); } Keys--; } } } public void checkReset() { if((Greenfoot.isKeyDown("r"))) { Greenfoot.setWorld(new Tutorial1()); } } }