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
// from the hero class World level2 = new Level2(); level2.addObject(this, /* x and y */); // transfer other objects Greenfoot.setWorld(level2);
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); } }
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; }
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); } }
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); } } }
//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; } } } }
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); } }
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); }
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); }