flixel icon indicating copy to clipboard operation
flixel copied to clipboard

Add support for multi-texture frame collections

Open EliteMasterEric opened this issue 3 years ago • 6 comments

For the purposes of optimization, it would be useful to have the ability to initialize a character's animations by creating a frame collection which is able to pull from two or more spritesheets.

For example, if you have some levels where a character needs some extra animations and other levels where these extra animations are not needed, you can place the primary animations in one Sparrow spritesheet and the secondary animations in an additional Sparrow spritesheet. Then the sprite would use the appropriate sprite and coordinates for the appropriate frame.

This is one manner it may appear to people using the feature:

var primaryFrameCollection:FlxAtlasFrames = FlxAtlasFrames.fromSparrow(primaryImage, primaryData);

var finalFrameCollection:FlxAtlasFrames = primaryFrameCollection;

if (needsSecondaryAnimations) {
  var secondaryFrameCollection = FlxAtlasFrames.fromSparrow(secondaryImage, secondaryData);
  finalFrameCollection= FlxAtlasFrames.concatenate([primaryFrameCollection, secondaryFrameCollection]);
}

sprite.frames = finalFrameCollection;

// Both of these should work.
sprite.play('animFromPrimaryCollection');
sprite.play('animFromSecondaryCollection');

In this case, we are able to create a sprite that includes both sets of animations, without secondaryImage having to include the frames from primaryImage.

EliteMasterEric avatar Jun 22 '22 01:06 EliteMasterEric

does sparrow always have a 1:1 image/data export? I thought there was a way to export multiple sheets with a single data file listing the frames of each image. otherwise I would expect different animations that share frames to export the same frame to multiple images.

Geokureli avatar Jun 22 '22 20:06 Geokureli

My recommendation is to make a standalone class that achieves this, I don't think it needs to be an overhaul of an existing class as this is pretty niche and only needed for giant animations with large frames, due to lime's bitmap limitations. If you can work out a standalone class that achieves this we could add it to flixel-addons. you could probably achieve this by extending FlxAtlasFrames or FlxAnimationController or maybe even FlxSprite

Geokureli avatar Jun 23 '22 01:06 Geokureli

You can achieve the desired result using

function addFrames(otherFrames:FlxFramesCollection, reload:Bool = true) {
	if(otherFrames == null) return;

	for(frame in otherFrames.frames) {
		this.frames.pushFrame(frame);
	}

	if(reload) {
		this.frames = this.frames;
	}
}

NeeEoo avatar Jul 09 '22 23:07 NeeEoo

@NeeEoo does this work when there are multiple images? I remember there being some issues with that.

@Geokureli I agree, this is a good target for Flixel addons

EliteMasterEric avatar Jul 09 '22 23:07 EliteMasterEric

Yea, it works with any amount, a sprite/character in a project im in uses 3 spritesheets (png + xml) due to the amount of frames it has.

NeeEoo avatar Jul 09 '22 23:07 NeeEoo

Ah that's interesting! I'll have to test it for myself. If it works, that looks simple enough to add to base Flixel without too much bloat.

On Sat, Jul 9, 2022, 19:31 NeeEoo @.***> wrote:

Yea, it works with any amount, a sprite/character in a project im in uses 3 spritesheets (png + xml) due to the amount of frames it has.

— Reply to this email directly, view it on GitHub https://github.com/HaxeFlixel/flixel/issues/2587#issuecomment-1179621293, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDLVRWETE6SVS4Q36XVBP3VTIDVXANCNFSM5ZOOAF2Q . You are receiving this because you authored the thread.Message ID: @.***>

EliteMasterEric avatar Jul 09 '22 23:07 EliteMasterEric

You can achieve the desired result using

function addFrames(otherFrames:FlxFramesCollection, reload:Bool = true) {
	if(otherFrames == null) return;

	for(frame in otherFrames.frames) {
		this.frames.pushFrame(frame);
	}

	if(reload) {
		this.frames = this.frames;
	}
}

Just a quick check-in, I tried this method and it 1000% worked as expected. Since frame collections can easily be reused between sprites, this is VERY powerful for improving efficiency.

I'll maybe make a PR to add this function to the FramesCollection proper then close this issue after I do that.

EliteMasterEric avatar Oct 20 '22 05:10 EliteMasterEric

@EliteMasterEric @SeiferTim have there been major breakthroughs in adding this functionality to flixel? Both of you said you needed to use this for a personal project, and both of your projects seem to be past the prototyping phase. If it's as straightforward as i hoped i would like to include it in the 5.3 release

Geokureli avatar Mar 09 '23 02:03 Geokureli

It's not any functionality that needs any significant work; you can already manually add frames (that possibly use a different graphic) and it'll work. Just adding something like the convenience function NeeEeo suggested would be sufficient since it's not obvious right now that you can do it.

EliteMasterEric avatar Mar 09 '23 03:03 EliteMasterEric

Btw, I had an issue with multi-texture frame collections via the method above in a project that featured a caching system (a simple Map<String, FlxGraphic> in which String is the path to the asset) where calling an animation from the secondary atlas would crash the game sometimes. I fixed it by also caching the secondary atlas's parent.

It might just be an issue with my project though, wanted to say this just in case

ACrazyTown avatar Mar 09 '23 15:03 ACrazyTown

@ACrazyTown is that project public? could you share some textures with some snippets showing the caching system you tried

Geokureli avatar Mar 09 '23 15:03 Geokureli

@ACrazyTown is that project public? could you share some textures with some snippets showing the caching system you tried

It isn't. I'll try to make a demo showcasing the issue (if I can reproduce it) in a couple days

ACrazyTown avatar Mar 09 '23 15:03 ACrazyTown