How do I make my actor die, if he falls down a hole, to the bottom of the world?
if (getY() == getWorld().getHeight() - 1) { getWorld().removeObject(this); return; }
import greenfoot.*; import java.awt.Color; public class Scoreboard extends Actor { int score = 0; public Scoreboard() { updateBoard(); } private void updateBoard() { setImage(new GreenfootImage("Score: " + score, 20, Color.black, new Color(0, 0, 0, 0))); } public void add(int addVal) { score += addVal; updateBoard(); } public int getScore() { return score; } }
import greenfoot.*; import java.awt.Color; public class Lifeboard extends Actor { int lives = 0; public Lifeboard() { updateBoard(); } private void updateBoard() { setImage(new GreenfootImage("Lives remaining: " + score, 20, Color.black, new Color(0, 0, 0, 0))); } public void add(int addVal) { if (lives == 0 && addVal == -1) { // ^ (lost a life and no lives remaining) // play sound, showGameOver/Final score // whatever you want to do for ending game Greenfoot.stop(); return; } lives += addVal; updateBoard(); } public int getLivesLeft() { return lives; } }
private void updateBoard() { setImage(new GreenfootImage("Lives remaining: " + score, 20, Color.black, new Color(0, 0, 0, 0))); }
public Scoreboard scoreboard = new Scoreboard(); public Lifeboard lifeboard = new Lifeboard();
addObject(scoreboard, 100, 20); addObject(lifeboard, 500, 20);
Arena arena = (Arena) getWorld(); Scoreboard sBoard = arena.scoreboard; sBoard.add(10); // adds 10 to the score