From c554cdb871a5bad1c557e1d7dbba4c73f77d8502 Mon Sep 17 00:00:00 2001 From: Shen Yiming Date: Wed, 18 Sep 2019 02:03:22 +0800 Subject: [PATCH] fix: Tagcount 0 eq. null bug This bug will result overwriting when multiple sprites are generated fix #8 --- bin/cli.js | 4 ++-- src/atlasify.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index c045a2c..966621d 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -123,7 +123,7 @@ if (opt.load) { function fileIO(result) { for (let a of result.atlas) { - const imageName = a.id ? `${a.name}.${a.id}.${a.ext}` : `${a.name}.${a.ext}` + const imageName = a.hasOwnProperty("id") ? `${a.name}.${a.id}.${a.ext}` : `${a.name}.${a.ext}` a.image.writeAsync(imageName) .then(() => { console.log(`Saved atlas: ${imageName}`); @@ -133,7 +133,7 @@ function fileIO(result) { }); } for (let s of result.spritesheets) { - const sheetName = s.id ? `${s.name}.${s.id}.${s.ext}` : `${s.name}.${s.ext}`; + const sheetName = s.hasOwnProperty("id") ? `${s.name}.${s.id}.${s.ext}` : `${s.name}.${s.ext}`; fs.writeFile(sheetName, result.exporter.compile(s), 'utf-8', err => { if(err) console.error(`Failed saving spritesheet ${sheetName}: ${err}`); else console.log(`Saved spritesheet: ${sheetName}`); diff --git a/src/atlasify.ts b/src/atlasify.ts index 98ba35d..be46fc7 100644 --- a/src/atlasify.ts +++ b/src/atlasify.ts @@ -284,7 +284,7 @@ export class Atlasify { this._packer.bins.forEach((bin, index: number) => { // Count tags let tag = bin.tag ? bin.tag : "_"; - if (!tagCount[tag]) tagCount[tag] = 0; // create index key if not exist + if (!tagCount.hasOwnProperty(tag)) tagCount[tag] = 0; // create index key if not exist else tagCount[tag]++; if (!bin.dirty) return; // early return if bin is not changed @@ -561,11 +561,11 @@ export class Atlasify { private pruneTagIndex (tagCount: { [index: string]: number; }) { for (let a of this._atlas) { const tag = a.tag ? a.tag : "_"; - if (tagCount[tag] < 2 && a.id) delete a.id; + if (tagCount[tag] < 1 && a.id) delete a.id; } for (let s of this._spritesheets) { const tag = s.tag ? s.tag : "_"; - if (tagCount[tag] < 2 && s.id) delete s.id; + if (tagCount[tag] < 1 && s.id) delete s.id; } } }