Workshop › Zelda-style Dungeon › Step 13 of 22
You have 3 hearts. An enemy touching you costs one. Then you are safe for 60 frames, and you and the enemy bounce apart, so it can't keep hitting you.
```js
let hearts
let hurt // frames the player cannot be hurt again
hearts = 3
hurt = 0
if (hurt > 0) hurt -= 1
if (e.hp > 0 && hurt === 0 && overlap(box(e), box(player))) {
hearts -= 1
hurt = 60
// Both bounce apart, so an enemy cannot stay stuck on top of you.
const ax = Math.sign(player.x - e.x) || 1
const ay = Math.sign(player.y - e.y)
const horizontal = Math.abs(player.x - e.x) >= Math.abs(player.y - e.y)
for (let i = 0; i < 6; i++) move(player, horizontal ? ax * 4 : 0, horizontal ? 0 : ay * 4)
e.knock = 10
e.kx = horizontal ? -ax * 3 : 0
e.ky = horizontal ? 0 : -ay * 3
}
```hurt counts down; while it is above 0, touches do nothing.ax and ay say which side of the enemy you are on (|| 1 when exactly on top).held, write hearts and hurt; in reset, set them above swing.update, count hurt down.Skills: Collision detection, Game states
← They wander Blinking and hearts →