hello i have a question, how can i switch between world classes with my hero class without lossing his stuff and without removing him from the world


1 2 3 4 5 | // from the hero class World level2 = new Level2(); level2.addObject( this , /* x and y */ ); // transfer other objects Greenfoot.setWorld(level2); |
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 | private static final Color textColor = new Color( 255 , 180 , 150 ); private int value = 0 ; private int target = 0 ; private String text; private int stringLength; private boolean coinFound = false ; public Counter() { this ( "" ); } public Counter(String prefix) { text = prefix; stringLength = (text.length() + 2 ) * 10 ; setImage( new GreenfootImage(stringLength, 16 )); GreenfootImage image = getImage(); image.setColor(textColor); updateImage(); } public void act() { if (value < target) { value++; updateImage(); } else if (value > target) { value--; updateImage(); } } public void add( int score) { target += score; } public int getValue() { return value; } /** * Make the image */ private void updateImage() { GreenfootImage image = getImage(); image.clear(); image.drawString(text + value, 1 , 12 ); } } |
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 | public class Dungeon_1 extends World { private Counter coinCounter; private Counter ammoCounter; private Counter healthCounter; /** * Constructor for objects of class Dungeon1. * */ public Dungeon_1() { super ( 800 , 600 , 1 ); setBackground( "dungeon_1 empty.png" ); createFloor(); generateWorld(); coinCounter = new Counter( "Coins: " ); addObject(coinCounter, 60 , 580 ); ammoCounter = new Counter( "Ammo: " ); addObject(ammoCounter, 60 , 560 ); healthCounter = new Counter( "Health: " ); addObject(healthCounter, 60 , 540 ); //Greenfoot.playSound("Overworld_Theme.wav"); } public Counter getCoinCounter() { return coinCounter; } public Counter getHealthCounter() { return healthCounter; } public Counter getAmmoCounter() { return ammoCounter; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public void pickUp() { Actor hero = getOneIntersectingObject(Hero. class ); if (pickingUp && hero == null ) // not touched anymore { pickingUp = false ; } else if (!pickingUp && hero != null ) // just being touched now { pickingUp = true ; Greenfoot.playSound( "LTTP_Item.wav" ); ((Dungeon_1)getWorld()).getAmmoCounter().add( 10 ); getWorld().removeObject( this ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public void pickUp() { Actor hero = getOneIntersectingObject(Hero. class ); if (pickingUp && hero == null ) // not touched anymore { pickingUp = false ; } else if (!pickingUp && hero != null ) // just being touched now { pickingUp = true ; Greenfoot.playSound( "LTTP_Rupee1.wav" ); ((Dungeon_1)getWorld()).getCoinCounter().add( 5 ); Greenfoot.playSound( "LTTP_Rupee2.wav" ); getWorld().removeObject( 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | //Player inventory private int ammo; private int rupees; boolean bigKey; boolean smallKey; //Timers private int reloadTimer; //Arrays for player animations private String[] playerUp = { "LinkUp1" , "LinkUp2" , "LinkUp3" , "LinkUp4" , "LinkUp5" , "LinkUp6" , "LinkUp7" }; private String[] playerDown = { "LinkDown1" , "LinkDown2" , "LinkDown3" , "LinkDown4" , "LinkDown5" , "LinkDown6" , "LinkDown7" }; private String[] playerRight = { "LinkRight1" , "LinkRight2" , "LinkRight3" , "LinkRight4" , "LinkRight5" , "LinkRight6" , "LinkRight7" }; //Constructor for standard player public Player() { //Stats maxHealth = 100 ; currentHealth = maxHealth; speed = 3 ; //Inventory ammo = 10 ; rupees = 0 ; bigKey = false ; smallKey = false ; //Timers invincibilityTimer = 0 ; reloadTimer = 0 ; //Inventory usage direction = 0 ; //Animation frame = 0 ; delayCount = 0 ; frameDelay = 4 ; spriteLength = playerUp.length; shiftup = true ; } //The player's actions public void act() { //From Character animationCycle(); wallCollision(); blockCollision(); invincibility(); //From Player checkInput(); reloadArrow(); grabPickup(); enterDoor(); enterLockedDoor(); //Hud //Hud hud = new Hud(getCurrentHealth(), getMaxHealth(), getAmmo(), getRupees()); } public void takeDamage( int damage) { if (!invincibility()) { currentHealth -= damage; if (currentHealth <= 0 ) { die(); } else { Greenfoot.playSound( "LTTP_Link_Hurt.wav" ); invincibilityTimer = 100 ; } } } //Check input from the user to move and perform actions, will show a non-walking sprite if no action is taken. public void checkInput() { if (Greenfoot.isKeyDown( "s" )) { swing(); } else if (Greenfoot.isKeyDown( "a" ) && ammo > 0 ) { shoot(); } else if (Greenfoot.isKeyDown( "up" )) { setLocation(getX(), getY()-speed); setImage(playerUp[frame]+ ".png" ); direction = 3 ; } else if (Greenfoot.isKeyDown( "left" )) { setLocation(getX()-speed, getY()); setImage(playerRight[frame]+ ".png" ); GreenfootImage img = getImage(); img.mirrorHorizontally(); setImage(img); direction = 2 ; } else if (Greenfoot.isKeyDown( "right" )) { setLocation(getX()+speed, getY()); setImage(playerRight[frame]+ ".png" ); direction = 0 ; } else if (Greenfoot.isKeyDown( "down" )) { setLocation(getX(), getY()+speed); setImage(playerDown[frame]+ ".png" ); direction = 1 ; } else { frame = 3 ; switch (direction) { case 0 : setImage(playerRight[frame]+ ".png" ); break ; case 1 : setImage(playerDown[frame]+ ".png" ); break ; case 2 : setImage(playerRight[frame]+ ".png" ); GreenfootImage img = getImage(); img.mirrorHorizontally(); setImage(img); break ; default : setImage(playerUp[frame]+ ".png" ); break ; } } } //Let's the player swing his sword public void swing() { switch (direction) { case 0 : setImage( "LinkSwingRight1.png" ); break ; case 1 : setImage( "LinkSwingDown1.png" ); break ; case 2 : setImage( "LinkSwingRight1.png" ); GreenfootImage img = getImage(); img.mirrorHorizontally(); setImage(img); break ; default : setImage( "LinkSwingUp1.png" ); break ; } invincibilityTimer = 2 ; } //Makes the player shoot an arrow public void shoot() { if (reloadTimer== 0 ) { Arrow arrow = new Arrow(direction); Greenfoot.playSound( "LTTP_Arrow_Shoot.wav" ); switch (direction) { case 0 : setImage( "LinkShootRight1.png" ); getWorld().addObject (arrow, getX()+ 10 , getY()); break ; case 1 : setImage( "LinkShootDown1.png" ); getWorld().addObject (arrow, getX(), getY()+ 10 ); break ; case 2 : setImage( "LinkShootRight1.png" ); GreenfootImage img = getImage(); img.mirrorHorizontally(); setImage(img); getWorld().addObject (arrow, getX()- 10 , getY()); break ; default : setImage( "LinkShootUp1.png" ); getWorld().addObject (arrow, getX(), getY()- 10 ); break ; } reloadTimer = 30 ; ammo--; } } //Player reloads his arrow public void reloadArrow() { if (reloadTimer > 0 ) { reloadTimer--; } } //Searches for pickups public void grabPickup() { Actor p = getOneIntersectingObject(Pickup. class ); if (p != null ) { Pickup pickup = (Pickup)p; switch (pickup.getItem()) { case 0 : Greenfoot.playSound( "LTTP_Item.wav" ); addAmmo( 5 ); break ; case 1 : Greenfoot.playSound( "LTTP_RefillHealth.wav" ); addHealth( 25 ); break ; default : Greenfoot.playSound( "LTTP_Rupee1.wav" ); addRupees( 50 ); break ; } getWorld().removeObject(p); } } //Restores the player's health public void addHealth( int amount) { currentHealth += amount; if (currentHealth > maxHealth) { currentHealth = maxHealth; } } //Adds ammo to the player's inventory public void addAmmo( int amount) { ammo += amount; } //Adds rupees to the player's inventory public void addRupees( int amount) { rupees += amount; } //Returns the player's current health public int getCurrentHealth() { return currentHealth; } //Returns the player's total health public int getMaxHealth() { return maxHealth; } //Returns the player's ammo public int getAmmo() { return ammo; } //Returns the player's rupees public int getRupees() { return rupees; } //When the player dies, an special sound and animation will show up public void die() { Greenfoot.playSound( "LTTP_Link_Dying.wav" ); Dead_Link dl = new Dead_Link(); getWorld().addObject (dl, getX(), getY()); getWorld().removeObject( this ); } public void enterDoor() { Actor od = getOneIntersectingObject(OpenDoor. class ); if (od != null ) { OpenDoor opendoor = (OpenDoor)od; switch (opendoor.getId()) { case 1 : World d2 = new Dungeon_2(); d2.addObject( this , 565 , 235 ); Greenfoot.setWorld(d2); break ; case 2 : World d3 = new Dungeon_3(); d3.addObject( this , 80 , 240 ); Greenfoot.setWorld(d3); break ; case 3 : World d4 = new Dungeon_1(); d4.addObject( this , 80 , 240 ); Greenfoot.setWorld(d4); break ; case 4 : World d5 = new Dungeon_1(); d5.addObject( this , 555 , 240 ); Greenfoot.setWorld(d5); break ; case 5 : World d6 = new Dungeon_4(); d6.addObject( this , 120 , 95 ); Greenfoot.setWorld(d6); break ; case 6 : World d7 = new Dungeon_3(); d7.addObject( this , 515 , 380 ); Greenfoot.setWorld(d7); break ; case 7 : World d8 = new Dungeon_1(); d8.addObject( this , 320 , 85 ); Greenfoot.setWorld(d8); break ; case 8 : World d9 = new Dungeon_1(); d9.addObject( this , 320 , 390 ); Greenfoot.setWorld(d9); break ; default : World d1 = new Dungeon_1(); d1.addObject( this , 320 , 390 ); Greenfoot.setWorld(d1); break ; } } } public void enterLockedDoor() { Actor ld = getOneIntersectingObject(LockedDoor. class ); if (ld != null ) { LockedDoor lockeddoor = (LockedDoor)ld; switch (lockeddoor.getId()) { case 1 : World d10 = new Dungeon_5(); d10.addObject( this , 320 , 390 ); Greenfoot.setWorld(d10); break ; case 2 : World d11 = new Dungeon_6(); d11.addObject( this , 320 , 390 ); Greenfoot.setWorld(d11); break ; default : World d1 = new Dungeon_1(); d1.addObject( this , 320 , 390 ); Greenfoot.setWorld(d1); break ; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class Hud extends Actor { public Hud( int currentHealth, int maxHealth, int ammo, int rupees) { GreenfootImage bg = getWorld().getBackground(); bg.setColor(Color.WHITE); bg.drawString( "Health: " + currentHealth + "/" + maxHealth + "\nAmmo: " + ammo + "\nRupees: " + rupees, 20 , 420 ); } } |
1 2 3 4 5 6 7 8 9 | public void addCounter() { coinCounter = new Counter( "Rupee: " ); getWorld().addObject(coinCounter, 48 , 464 ); ammoCounter = new Counter( "Ammo: " ); getWorld().addObject(ammoCounter, 48 , 448 ); healthCounter = new Counter( "Health: " ); getWorld().addObject(healthCounter, 48 , 432 ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public Player() { // code you already have coinCounter = new Counter( "Rupee: " ); ammoCounter = new Counter( "Ammo: " ); healthCounter = new Counter( "Health: " ); } public void addedToWorld(World world) { world.addObject(coinCounter, 48 , 464 ); world.addObject(ammoCounter, 48 448 ); world.addObject(healthCounter, 48 , 432 ); } |