Is there a quick code to greenfoot to where if the player gets hit twice then the object will die?
I want it to where you have to shoot the object twice then it will die.
// using int field for any number of hits (starting at one) int hitsToDie = /* some number */; int hitsTaken = 0; // method called when hit public void hit() { hitsTaken++; if (hitsTaken == hitsToDie) getWorld().removeObect(this); }
// using a boolean when only two hits required to die boolean hasTakenAHit; // method called when hit public void hit() { if (hasTakenAHit) getWorld().removeObject(this); else hasTakenAHit = true; }
public void fireBallHit() { Actor b = getOneIntersectingObject(Alien.class); if(b != null) { getWorld().removeObject(b); getWorld().removeObject(this); } }
public void fireBallHit() { Actor b = getOneIntersectingObject(Alien.class); if(b != null) { //here hitsTaken++; getWorld().removeObject(b); getWorld().removeObject(this); } }
if(hitsTaken==hitsToDie) { getWorld().removeObject(this); }
if(this!=null) { //all code in act method }
private boolean hasTakenAHit; public void fireBallHit() { Actor b = getOneIntersectingObject(Alien.class); if (b != null) { getWorld().removeObject(b); if (hasTakenAHit) getWorld().removeObject(this); hasTakenAHit = true; } }