# Creating a shield for an enemy class

**URL:** https://discourse.processing.org/t/creating-a-shield-for-an-enemy-class/31242
**Category:** Coding Questions
**Created:** [July 13, 2021, 10:23pm UTC](https://discourse.processing.org/t/creating-a-shield-for-an-enemy-class/31242 "2021-07-13T22:23:07Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Gyrosis](https://avatars.discourse-cdn.com/v4/letter/g/aeb1de/32.png) [@Gyrosis](https://discourse.processing.org/u/Gyrosis)
#### Post date: [July 13, 2021, 10:23pm UTC](https://discourse.processing.org/t/creating-a-shield-for-an-enemy-class/31242/1 "2021-07-13T22:23:07Z")

</div>

So say if I want to create a shield so that you would have to take down the shield before killing the enemy. How would I code that?

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [July 14, 2021, 7:08am UTC](https://discourse.processing.org/t/creating-a-shield-for-an-enemy-class/31242/2 "2021-07-14T07:08:24Z")

</div>

If the enemies are invulnerable before you destroy the shield you could do something like

```auto
//enemy die check
if(hasShield==false&&lives<0) destroyed();

```

otherwise you can just do something like

```auto
void damaged(int dmg) {
   if(shieldPoints>0) shieldPoints -= dmg;
   if(shieldPoints<0) {
       lifePoints+=shieldPoints; //since shield points are less than 0, so addition will lower the number     
      shieldPoints = 0;
   } else lifePoints-=dmg;
}
//only display shield if shieldPoints>0

```

EDIT: there is a bug. I should not have used `else lifePoints-=dmg` since that would never happen with shieldPoints equal or lower to 0. I fixed it now, so the condition is `shieldPoints<0` instead of \<=
