0
\$\begingroup\$

I'm creating a basic animation system, where I call functions to change the rendered sprite and turn on hitboxes based on the current frame of the animation.

For example, if (current_frame == 8), I turn on the hitbox. Then, if it hits something or if current_frame == 9, the hitbox is turned off.

However I've stumbled into a problem. If I implement an attack speed modifier, like, 20% attack speed increase, on 7th game update, the current_frame is 8.4 and I miss the function call to turn on the hitbox. I know I can implement something like (current_frame >= 8 && <= 9), but this doesn't work If I add an attack speed multipler of 100% or more - the frame will be skipped and functions wouldn't be called.

I'm also open for new animation systems / books / articles to study!

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Option 1: Have a second, integral frame counter.

After updating the original counter, gradually increment this new counter to match it, executing all your logic along the way:

for (; current_frame_int + 1 <= current_frame; current_frame_int++)
{
    // Add all your frame-based logic here.
}

Option 2: Have a flag to tell you if a specific step is already finished.

bool hitbox_shown = false;
if (current_frame >= 8 && !hitbox_shown)
{
    hitbox_shown = true;
    ShowHitbox();
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .