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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | //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 ; } |
1 2 3 4 5 6 7 8 9 10 11 12 | public void checkHeroCollision(){ Actor collided = getOneIntersectingObject(Hero. class ); if (collided != null ){ Hero.shield(); getWorld().removeObject( this ); return ; } else { if (getX() <= - outOfBoundary) { getWorld().removeObject( this ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 | 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 ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 ); } } } |