0
\$\begingroup\$

I am building an 2D Engine in JavaScript and if my actor attacks an enemy, a hitbox appears for a certain amount of time and then disappears. If the target keeps standing inside of the hitbox he gets drained down for each frame. So for example if the hitbox does 3 points damage and the target only has 9 HP he will be dead after 3 frames. That's too fast.

I've come up with two solutions, both of them are not ideal:

  1. Set the enemy as unattackable for a certain amount of time after each hit.
  2. Save an reference to the enemy that gets deleted after the hitbox disappears.

My explanation is pretty bad but posting my code would be worse since it's way too much.

\$\endgroup\$
1
\$\begingroup\$

Your option 1 (invulnerability timer) is what many classic (and even modern) games do.

A variant of your option 2 is a better approach in some respects, though "better" depends on your desired gameplay (some games actually depend on the invulnerability timer mechanics for puzzle solving or the like, though IMO that's horrible). Especially if you have multiple players or the like, letting each damage the enemy simultaneously can be nice. The basic gist is for each attack state to simply store a list of objects it hit and the times. You can then ignore all further collision detections for that attack or have a per-attack timer (for attacks that are supposed to do damage over time). This list could be per-attacker rather than per-attack if that makes it simpler.

\$\endgroup\$
0
\$\begingroup\$

Depending what you consider ideal and the nature of your engine / update loop, the actor's attack hitbox could be present for exactly one frame. After the actor attacks, it could then be subject to a period of recovery before attacking again.

This method would come with the following behavior:

  • player attacks once when hitting a key or clicking the mouse
  • any or multiple creatures within the player's attack are hit
  • the player then must wait for stamina or attack cooldown (or whatever) before attacking again

Some things to consider about this method:

  • by default, the attack would be calculated at the beginning of the attack animation
  • to make the attack appear to have duration, the attack animation would need to begin, and then a few frames later the attack hitbox would be placed and used
  • this form of attack cannot 'move' across the screen like a projectile (though it would be fine for something like a laser)
\$\endgroup\$
  • \$\begingroup\$ Actually i considered this too. But it seemed a bit unnatural to the Animations i used for my Attacks. I just hoped there was a very simple way to do this that i could not come up with. \$\endgroup\$ – Mottenmann Dec 3 '13 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.