From 423da12c3c2a176d2d7573d147c32f8f59b86bf4 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Thu, 18 May 2017 13:56:14 -0400 Subject: [PATCH] Handle more cases of deleting costumes --- src/sprites/rendered-target.js | 6 ++-- test/unit/sprites_rendered-target.js | 46 ++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 1e7ff9a2e..06528ece0 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -401,10 +401,10 @@ class RenderedTarget extends Target { .slice(0, index) .concat(this.sprite.costumes.slice(index + 1)); - if (index === originalCostumeCount - 1) { + if (index === this.currentCostume && index === originalCostumeCount - 1) { this.setCostume(index - 1); - } else { - this.setCostume(index); + } else if (index < this.currentCostume) { + this.setCostume(this.currentCostume - 1); } this.runtime.requestTargetsUpdate(this); diff --git a/test/unit/sprites_rendered-target.js b/test/unit/sprites_rendered-target.js index 5e7f881b1..ecb4a0899 100644 --- a/test/unit/sprites_rendered-target.js +++ b/test/unit/sprites_rendered-target.js @@ -102,6 +102,8 @@ test('deleteCostume', t => { const o1 = {id: 1}; const o2 = {id: 2}; const o3 = {id: 3}; + const o4 = {id: 4}; + const o5 = {id: 5}; const s = new Sprite(); const r = new Runtime(); @@ -110,20 +112,26 @@ test('deleteCostume', t => { const renderer = new FakeRenderer(); a.renderer = renderer; - // Deleting costume keeps costume index at 0 + // x* Costume 1 * Costume 2 + // Costume 2 => Costume 3 + // Costume 3 a.setCostume(0); a.deleteCostume(0); t.deepEqual(a.sprite.costumes, [o2, o3]); t.equals(a.currentCostume, 0); - // Deleting a costume in the middle maintains current costume index + // Costume 1 Costume 1 + // x* Costume 2 => * Costume 3 + // Costume 3 a.sprite.costumes = [o1, o2, o3]; a.setCostume(1); a.deleteCostume(1); t.deepEqual(a.sprite.costumes, [o1, o3]); t.equals(a.currentCostume, 1); - // Deleting last costume selects previous costume + // Costume 1 Costume 1 + // Costume 2 => * Costume 2 + // x* Costume 3 a.sprite.costumes = [o1, o2, o3]; a.setCostume(2); a.deleteCostume(2); @@ -137,6 +145,38 @@ test('deleteCostume', t => { t.deepEqual(a.sprite.costumes, [o1]); t.equals(a.currentCostume, 0); + // Costume 1 Costume 1 + // x Costume 2 Costume 3 + // Costume 3 => * Costume 4 + // * Costume 4 Costume 5 + // Costume 5 + a.sprite.costumes = [o1, o2, o3, o4, o5]; + a.setCostume(3); + a.deleteCostume(1); + t.deepEqual(a.sprite.costumes, [o1, o3, o4, o5]); + t.equals(a.currentCostume, 2); + + // Costume 1 Costume 1 + // * Costume 2 * Costume 2 + // Costume 3 => Costume 3 + // x Costume 4 Costume 5 + // Costume 5 + a.sprite.costumes = [o1, o2, o3, o4, o5]; + a.setCostume(1); + a.deleteCostume(3); + t.deepEqual(a.sprite.costumes, [o1, o2, o3, o5]); + t.equals(a.currentCostume, 1); + + // Costume 1 Costume 1 + // * Costume 2 * Costume 2 + // Costume 3 => Costume 3 + // Costume 4 Costume 4 + // x Costume 5 + a.sprite.costumes = [o1, o2, o3, o4, o5]; + a.setCostume(1); + a.deleteCostume(4); + t.deepEqual(a.sprite.costumes, [o1, o2, o3, o4]); + t.equals(a.currentCostume, 1); t.end(); });