how would I go about implementing a shield in my game? I have included a boolean on the hero to activate the shield but not too sure on the collision and health bar workings. link to my game: http://www.greenfoot.org/scenarios/5799
public void checkHeroCollision(){
Actor collided = getOneIntersectingObject(Hero.class);
if (collided != null){
City cityWorld = (City) getWorld(); // get a reference to the world
if(!Hero.shieldActive){ // is your hero shield not active? Then take damage
Bar bar = cityWorld.getBar(); // get a reference to the counter
bar.subtract(10);
}
getWorld().addObject(new explosionSmall(), getX(), getY());
getWorld().removeObject(this);
return;
} else {
if (getX() <= - outOfBoundary) {
getWorld().removeObject(this);
}
}
}
static boolean shieldActive;
private int shieldCounter = 0;
public void shield(){
shieldActive = true;
setImage("shield.jpg");
shieldCounter ++;
if (shieldCounter == 100) {
setImage("Hero.png");
shieldCounter = 0;
shieldActive = false;
}
}
//in the act method
if(shieldCounter>0) shieldCounter--;
if(shieldCounter==0) {
setImage("Hero.png");
shieldActive = false;
}
// this is called once
public void shield(){
shieldActive = true;
setImage("shield.jpg");
shieldCounter = 100;
}
public void checkHeroCollision(){
Actor collided = getOneIntersectingObject(Hero.class);
if (collided != null){
Hero.shield();
getWorld().removeObject(this);
return;
} else {
if (getX() <= - outOfBoundary) {
getWorld().removeObject(this);
}
}
}
public void checkHeroCollision(){
Hero collided = (Hero) getOneIntersectingObject(Hero.class);
if (collided != null){
collided.shield();
getWorld().removeObject(this);
return;
} else {
if (getX() <= - outOfBoundary) {
getWorld().removeObject(this);
}
}
}
public void checkHeroCollision(){
Actor collided = getOneIntersectingObject(Hero.class);
if (collided != null){
City cityWorld = (City) getWorld(); // get a reference to the world
if(!Hero.shieldActive){ // is your hero shield not active? Then take damage
Bar bar = cityWorld.getBar(); // get a reference to the counter
bar.subtract(10);
}
getWorld().addObject(new explosionSmall(), getX(), getY());
getWorld().removeObject(this);
return;
} else {
if (getX() <= - outOfBoundary) {
getWorld().removeObject(this);
}
}
}