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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 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 ); } } } } |