spritesheet.js icon indicating copy to clipboard operation
spritesheet.js copied to clipboard

Can't put sprites in order

Open CloverFeywilde opened this issue 8 years ago • 2 comments

For some reason mine just refuses to put my spritesheet together in numerical order. It tries but has a few bugs in how it orders numbers.

For example... with files labeled 00 - 41 it will output them 00, 21, 02, 03, instead of 00, 01, 02, 03

CloverFeywilde avatar Nov 06 '16 00:11 CloverFeywilde

I'm not the maintainer, just someone looking for the same answer as you. So, the packer uses some algorithm to try and fit the images in the best it can. In the case where the images are all the same size, the ordering seems to be haphazard.

I make spritesheets for use in Phaser/PIXI.js, and in those cases, I was able to specify the sprites by key instead of by index, like so:

me.animations.add('one', ['bullet-1.png', 'bullet-2.png'], 5, true);

Instead of:

me.animations.add('one', [0, 1], 5, true);

So, if there's a way in your framework to reference things by key, that would be one way to go. I don't know if this will help your situation or not, unfortunately.

I did try and hack the Sorter class in spritesheet-js to no avail (the packer seems to do what it wants anyway).

rjungemann avatar Nov 22 '16 06:11 rjungemann

Hey there, I think I've found the root of the problem.

Even if you're not using "CSS" as export format, the generator is re-ordering the files before generating the JSON output, based on a rule only valid for CSS: https://github.com/krzysztof-o/spritesheet.js/blob/2757c39f6f17c8a09105082adae646346504fecc/lib/generator.js#L220-L222

As a workaround, I've added a condition in a local version of spritesheet-js. This fixed a problem with ordering I was having with the CreateJS format.

// ...
if (options.format === "css") {
	options.files.sort(function(a, b) {
		return a.cssPriority - b.cssPriority;
	});
}
// ...

Created a pull-request for this here: https://github.com/krzysztof-o/spritesheet.js/pull/56

endel avatar Feb 13 '17 14:02 endel