Skip to content

Conversation

@dtwotwo
Copy link
Contributor

@dtwotwo dtwotwo commented Feb 10, 2026

fixes bugs in FixAnimationController

  • corrected off-by-one error in add() frame validation
    (while (i-- >= 0) -> while (i-- > 0))
  • fixed negative frame index handling in set_frameIndex:
    negative values are now clamped to 0 with a log warning,
    values >= numFrames are wrapped with a log warning

@dtwotwo dtwotwo changed the title fix off-by-one and negative modulo bugs in FlxAnimationController FlxAnimationController: fix off-by-one and negative modulo bugs Feb 10, 2026
@Geokureli
Copy link
Member

Geokureli commented Feb 10, 2026

For posterity, the off-by-one bug was introduced here: #2762 (D'oh!)

Can you provide examples that would have failed in prior versions? We should add unit tests for them. I'm also not sure I understand how negative frame indices are used in set_frameIndex

fix frame indexing and modulo behavior
@dtwotwo
Copy link
Contributor Author

dtwotwo commented Feb 10, 2026

For posterity, the off-by-one bug was introduced here: #2762 (D'oh!)

Can you provide examples that would have failed in prior versions? We should add unit tests for them. I'm also not sure I understand how negative frame indices are used in set_frameIndex

off-by-one in add():

the loop while (i-- >= 0) fires an extra iteration on every add() call. when i is 0, the condition 0 >= 0 is true, i becomes -1, and the body accesses framesToAdd[-1]. on cpp this is an out-of-bounds read that corrupts the array. The same file already uses the correct pattern in removeInvalidFrames - while (i-- > 0).

negative modulo in set_frameIndex:

frameIndex is a public property, and internally copyFrom can pass the default value of -1 into it. in Haxe, -1 % 4 == -1, not 3, so _sprite.frames.frames[-1] is an out-of-bounds access. FlxMath.mod handles wrapping correctly.

unit tests:

added testAddAllValidFrames, testAddSomeInvalidFrames, testAddSingleValidFrame, testAddAllInvalidFrames, testFrameIndexNegativeWraps for test possible fallbacks

@Geokureli Geokureli merged commit 203f45d into HaxeFlixel:dev Feb 11, 2026
10 checks passed
@Geokureli
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants