Problem: My scorecounter keeps track of mobs killed. If mobs killed in myWorld is 3, then the score resets to 0 and world level2 is created. However when the score again reaches 3, it resets level2. Why is this?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; /** * Main Player class * * @author * @version v1.0-beta */ public class player extends Actor { private int energy = 300; //player energy (used when pressing space private int health = 100; //player health private int coolDown = 100; //cooldown for blocks private int enmHealth = 100; //enemy health track private int numOfBars = 200; //number of temp blocks private int level = 1; //player current level /* * @author Agniva * @description Pretty much the main method */ public void act() { controls(); getWorld().showText("Energy: " + energy/10, 55,30); setLocation(getX(), getY()); eat(); getEaten(); } /* * @author Agniva * @description Player attack simulations */ public void getEaten(){ Actor enemy; enemy= getOneObjectAtOffset(0,0,mob.class); if(enemy !=null){ health -=5; Greenfoot.delay(5); getWorld().showText("Health: " + health, 55,15); if(health==0){ World world; world = getWorld(); getWorld().showText("GAME OVER", 75 , 100); world.removeObject( this ); Greenfoot.stop(); } } } /* * @author Agniva * @description Eating process of all food for player */ public void eat(){ Actor fuud; fuud = getOneObjectAtOffset(0,0,food.class); if(fuud !=null){ getWorld().removeObject(fuud); energy +=100; health +=10; getWorld().showText("Health: " + health, 55,15); } } /* * @author Agniva * @param Valid X/Y Coords * @description Prevents movability of worldBricks for player */ public void setLocation(int x, int y){ if(getWorld().getObjectsAt(x,y,worldBrick.class).isEmpty()){ super.setLocation(x,y); } } /* * @author Agniva * @description Controls player given keyboard inputs only */ public void controls(){ if(Greenfoot.isKeyDown("a")){ turn(-2); } if(Greenfoot.isKeyDown("d")){ turn(2); } if(Greenfoot.isKeyDown("w")){ if(Greenfoot.isKeyDown("space")&& (energy != 0)){ move(4); energy--; } else{ move(2); } } if(Greenfoot.isKeyDown("s")){ if(Greenfoot.isKeyDown("space") && (energy != 0)){ move(-4); energy--; } else{ move(-2); } } //if 1 is pressed, brick is placed. if(Greenfoot.isKeyDown("q")){ if(coolDown == 1000 && numOfBars >0){ playerBrick br = new playerBrick(); getWorld().addObject(br, getX()+2, getY()+2); coolDown = 0; numOfBars--; getWorld().showText("Blocks: " + numOfBars, 60 , 70); } else{ while(coolDown !=1000){ coolDown++; } } } //if button 2 is pressed, enemy health is reduced by 50%. //enough health will restore next to 100 if(Greenfoot.isKeyDown("e")){ Actor enemy; ArrayList<scoreCount> scoreD = new ArrayList<scoreCount>(); for (scoreCount obj : getWorld().getObjects(scoreCount.class)) scoreD.add((scoreCount)obj); enemy= getOneObjectAtOffset(0,0,mob.class); scoreCount object = scoreD.get(0); if(enemy !=null){ enmHealth-=50; if(enmHealth <0){ getWorld().removeObject(enemy); energy +=50; enmHealth = 100; object.incrementScore(); } } if(object.score() == 3 && level == 1){ level++; object.setScore(0); level2 world2 = new level2(); Greenfoot.setWorld(world2); } } if(Greenfoot.isKeyDown("r")){ Actor bricker; bricker = getOneObjectAtOffset(0, 0, worldBrick.class); if(isTouching(worldBrick.class)){ removeTouching(worldBrick.class); numOfBars +=50; getWorld().showText("Blocks: " + numOfBars, 60 , 70); } } } }