Using several tile sheets with the same sprite

40 views
Skip to first unread message

cubixpix

unread,
Oct 14, 2015, 9:12:31 AM10/14/15
to haxef...@googlegroups.com
Hello,

I'd like to use several sprite sheets for the same sprite.

It can be done by using loadGraphic function in the sprite class.
This function loads once from disk, and put the data in memory cache, for the future call.
It stays in cache as long as there are no state switching.

This is fine for me: i use loadGraphic when i need to change animation.

The problem is, it seems that everytime i use loadGraphic , i must call animation.add because all animations are reset !

Have i to do something like this, when i want to change sprite animation ?


    loadGraphicFromSprite(hero_walk);
    animation.add("walk", [for (i in 0 ... 13) i], 24, false);



I have read that animation list was preserved between the loadGraphic but it's not the case for me.
If i do something like:

    animation.add("walk", [for (i in 0 ... 13) i], 24, false);
    animation.add("run", [for (i in 0 ... 8) i], 24, false);
    animation.add("jump", [for (i in 0 ... 8) i], 24, false);

... followed by a loadGraphic , then all the animations are reset.

I can of course do the animation.add each time i reload a sprite sheet, but it seems wasted resources for me.

Can you help ?

Thank you.

SruloArt

unread,
Oct 15, 2015, 5:39:13 AM10/15/15
to HaxeFlixel
* Where did you read that the animation list is preserved? https://github.com/HaxeFlixel/flixel/blob/master/flixel/FlxSprite.hx#L345

cubixpix

unread,
Oct 15, 2015, 5:54:54 AM10/15/15
to HaxeFlixel


* Where did you read that the animation list is preserved? https://github.com/HaxeFlixel/flixel/blob/master/flixel/FlxSprite.hx#L345


I read it here:

http://forums.flixel.org/index.php?topic=7043.0

 wg/funstorm has written in the third message:


Don't redo the animations when using loadGraphic again. The animations are not cleared when using loadGraphic, so it will still have any previous animations and will just play the same frame numbers but using the new graphic. When you do addAnimation, the animation is simply added to the end of the animation list, so if you were to add new animations each time you change graphic you would be adding the same animation again to the end of the animation array. No need.

BTW, i've a question about the Unique field in loadGraphic.

The doc says:

Unique

Optional, whether the graphic should be a unique instance in the graphics cache. Default is false.


Does it mean that the tilesheet won't be put in cache is Unique is set to true ?

Gama11

unread,
Oct 15, 2015, 6:57:57 AM10/15/15
to haxef...@googlegroups.com
That forum is for AS3 Flixel, so the information there isn't always relevant for HaxeFlixel. This is quite a hidden feature (more of a sideeffect than anything really) so it probably worked in some earlier version of HaxeFlixel that were closer to AS3 Flixel, but doesn't work anymore because of changes to the graphics system (since it's unlikely somebody knew about this "feature"). Might be possible to make it work again though if there's a use case for it (or via some other way that isn't as counter intuitive).

Unique graphics are technically still in the cache (and thus visible with viewCache), but if you load the same graphic on another sprite, it won't use the same instance, so you'd end up with two copies of the same graphic in memory. This can be useful if you want to manipulate the graphic, for instance with FlxSpriteUtil.

cubixpix

unread,
Oct 15, 2015, 2:31:39 PM10/15/15
to HaxeFlixel
Ok i understand now, why the animation list was reset.

Create it each time i call loadGraphic shouldn't be a big deal.

I've had a look to loadGraphic itself. It calls another function, which just returns the result of addWithSpaces.

 public function addWithSpaces(Graphic:Dynamic, FrameWidth:Int, FrameHeight:Int, SpacingX:Int = 1, SpacingY:Int = 1, Unique:Bool = false, ?Key:String):CachedGraphics
       
{
               
if (Graphic == null)
               
{
                       
return null;
               
}
               
               
// (i've cut a bit , here comes interesting part)
               
               
if (Std.is(Graphic, CachedGraphics))
               
{
                        isGraphics
= true;      
                        graphic
= cast(Graphic, CachedGraphics);
                       
                       
if (!Unique && (FrameWidth <= 0 && FrameHeight <= 0))
                       
{
                               
return graphic;
                       
}
               
}


So we can see that if Graphic id is known in the cache, and the bitmap is not unique, then the data in cache is returned immediatly.
Not a big deal on performances hit, i guess. Dunno what the test with FrameWidth and FrameHeight is.

Thank you for the explanations

marked as solved

Reply all
Reply to author
Forward
0 new messages