diff --git a/cocoscreator_projects/scripts/test/ui-frames.test.mjs b/cocoscreator_projects/scripts/test/ui-frames.test.mjs index 5801dd6..515862d 100644 --- a/cocoscreator_projects/scripts/test/ui-frames.test.mjs +++ b/cocoscreator_projects/scripts/test/ui-frames.test.mjs @@ -44,6 +44,16 @@ test('frameRect 越界抛 RangeError 且带图名', () => { assert.throws(() => frameRect(IMG_00014, 13), /00014\.png/); }); +test('frameRect undefined 作为 frameIndex 抛 RangeError', () => { + assert.throws(() => frameRect(IMG_00014, undefined), RangeError); + assert.throws(() => frameRect(IMG_00014, undefined), /00014\.png/); +}); + +test('frameRect NaN 作为 frameIndex 抛 RangeError', () => { + assert.throws(() => frameRect(IMG_00014, NaN), RangeError); + assert.throws(() => frameRect(IMG_00014, NaN), /00014\.png/); +}); + test('padWidth 按帧总数决定补零位数', () => { assert.equal(padWidth(9), 1); assert.equal(padWidth(12), 2); @@ -61,6 +71,14 @@ test('frameFileName 单帧图原样返回', () => { assert.equal(frameFileName('00001.png', 1, 1), '00001.png'); }); +test('frameFileName undefined 作为 frameIndex 抛 RangeError', () => { + assert.throws(() => frameFileName('00014.png', undefined, 12), RangeError); +}); + +test('frameFileName NaN 作为 frameIndex 抛 RangeError', () => { + assert.throws(() => frameFileName('00014.png', NaN, 12), RangeError); +}); + test('frameNames 返回全部帧,下标 0 对应帧 1', () => { const names = frameNames(IMG_00014); assert.equal(names.length, 12); diff --git a/cocoscreator_projects/scripts/ui-migration/frames.mjs b/cocoscreator_projects/scripts/ui-migration/frames.mjs index 2dc113a..acf0a84 100644 --- a/cocoscreator_projects/scripts/ui-migration/frames.mjs +++ b/cocoscreator_projects/scripts/ui-migration/frames.mjs @@ -5,10 +5,11 @@ /** 帧号 → 源矩形。FrameIndex=0 按帧 1 处理(实测仅 2 个对象如此)。 */ export function frameRect(image, frameIndex) { - const idx = (frameIndex || 1) - 1; - if (idx < 0 || idx >= image.frame_all) { + const normalized = frameIndex === 0 ? 1 : frameIndex; + const idx = normalized - 1; + if (!Number.isInteger(idx) || idx < 0 || idx >= image.frame_all) { throw new RangeError( - `帧号越界: ${image.bmp} frameIndex=${frameIndex}(有效范围 1..${image.frame_all})`, + `帧号非法或越界: ${image.bmp} frameIndex=${frameIndex}(有效范围 1..${image.frame_all},0 视同 1)`, ); } return { @@ -27,8 +28,14 @@ export function padWidth(frameCount) { /** 00014.png + 帧 5/共 12 → 00014_05.png;单帧图原样返回。 */ export function frameFileName(bmp, frameIndex, frameCount) { if (frameCount <= 1) return bmp; + const normalized = frameIndex === 0 ? 1 : frameIndex; + if (!Number.isInteger(normalized) || normalized <= 0) { + throw new RangeError( + `帧号非法: ${bmp} frameIndex=${frameIndex}(有效范围 >= 1,0 视同 1)`, + ); + } const base = bmp.replace(/\.png$/i, ''); - const n = String(frameIndex || 1).padStart(padWidth(frameCount), '0'); + const n = String(normalized).padStart(padWidth(frameCount), '0'); return `${base}_${n}.png`; }