Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative animation frame number #3028

Merged
merged 4 commits into from
Feb 10, 2024
Merged

Conversation

T1mL3arn
Copy link
Contributor

@T1mL3arn T1mL3arn commented Feb 9, 2024

I got in the situation when animations play backwards despite in aseprite editor they are clearly normal. The reason was that I used format for exported frames like this bla-bla-{frame} and prefix to add the anim like bla-bla. With this combination the frames are treated as negative integers, which results with reversed frame order.

I think there is no such thing as "negative frame numbers" in spritesheet animations, so here is the fix.

@Geokureli
Copy link
Member

Geokureli commented Feb 9, 2024

Can you include a test case that reproduces the issue? At a glance it should like your prefix is "bla-bla-" but you're saying it's "bla-bla" without the final dash

@T1mL3arn
Copy link
Contributor Author

but you're saying it's "bla-bla" without the final dash

Yes, the prefix is "bla-bla". That's why "-4" number substring is extracted from a frame name like "bla-bla-4".

@T1mL3arn
Copy link
Contributor Author

Example

var sprite = new FlxSprite();

// plain frame data from atlas json
var framesSource = {
	"frames": [
		{
			"filename": "top-right-walk-1",
			"frame": {
				"x": 71,
				"y": 0,
				"w": 71,
				"h": 156
			},
			"rotated": false,
			"trimmed": true,
			"spriteSourceSize": {
				"x": 29,
				"y": 20,
				"w": 71,
				"h": 156
			},
			"sourceSize": {
				"w": 128,
				"h": 192
			},
			"duration": 200
		},
		{
			"filename": "top-right-walk-2",
			"frame": {
				"x": 142,
				"y": 0,
				"w": 71,
				"h": 156
			},
			"rotated": false,
			"trimmed": true,
			"spriteSourceSize": {
				"x": 29,
				"y": 20,
				"w": 71,
				"h": 156
			},
			"sourceSize": {"w": 128, "h": 192},
			"duration": 200
		},
	]
};

// just building frames, skipping loading assets steps
var atlasFrames = new FlxAtlasFrames(FlxG.bitmap.add(new BitmapData(100, 100), false));
for (frame in framesSource.frames)
{
	@:privateAccess
	FlxAtlasFrames.texturePackerHelper(frame.filename, cast frame, atlasFrames, true);
}
sprite.frames = atlasFrames;

// add two animations from the same frames, using different prefixes
sprite.animation.addByPrefix('walk-incorrect', 'top-right-walk');
sprite.animation.addByPrefix('walk-correct', 'top-right-walk-');

var incorrect = sprite.animation.getByName('walk-incorrect');
var correct = sprite.animation.getByName('walk-correct');

trace('anim frames indicies  BAD: ${incorrect.frames}');
trace('anim frames indicies GOOD: ${correct.frames}');

// anim frames indicies  BAD: [1,0]
// anim frames indicies GOOD: [0,1]

@Geokureli
Copy link
Member

Geokureli commented Feb 10, 2024

In this case I would say the fix is to put the dash in the prefix since the prefix is "top-right-walk-" but I doubt people will try to use negative frame numbers. On the off-chance that someone does I've added warning logs. I've also fixed some existing things that have been bugging me, like wtf is a "postFix", lol

@Geokureli
Copy link
Member

Thanks!

@Geokureli Geokureli merged commit 1e3c39f into HaxeFlixel:dev Feb 10, 2024
16 checks passed
@Geokureli Geokureli added this to the 5.7.0 milestone Feb 10, 2024
Geokureli added a commit that referenced this pull request Feb 10, 2024
* Added `letterSpacing` variable to `FlxText`. (#3027)

* Update FlxText.hx

* `spacing` -> `letterSpacing`.

* Oops

* Update FlxText.hx

* Update FlxText.hx

* Fix negative animation frame number (#3028)

* fix negative anim frame number sorting

* use int abs

* warn when sorting frames with invalid names

* remove typo

---------

Co-authored-by: George FunBook <gkurelic@gmail.com>

* downgrade warnings and prevent more crashes

* fix error in xml attribute check

* pretty up unit test

---------

Co-authored-by: Mihai Alexandru <77043862+MAJigsaw77@users.noreply.github.com>
Co-authored-by: Timur <T1mL3arn@users.noreply.github.com>
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.

None yet

2 participants