diff --git a/src/blocks/scratch3_looks.js b/src/blocks/scratch3_looks.js index 6addc629..bd885dbf 100644 --- a/src/blocks/scratch3_looks.js +++ b/src/blocks/scratch3_looks.js @@ -353,7 +353,7 @@ class Scratch3LooksBlocks { */ _setCostume (target, requestedCostume, optZeroIndex) { if (typeof requestedCostume === 'number') { - // Numbers should be treated as comments, always + // Numbers should be treated as costume indices, always target.setCostume(optZeroIndex ? requestedCostume : requestedCostume - 1); } else { const costumeIndex = target.getCostumeIndexByName(requestedCostume.toString()); @@ -386,16 +386,17 @@ class Scratch3LooksBlocks { */ _setBackdrop (stage, requestedBackdrop, optZeroIndex) { if (typeof requestedBackdrop === 'number') { + // Numbers should be treated as backdrop indices, always stage.setCostume(optZeroIndex ? requestedBackdrop : requestedBackdrop - 1); - } else if (requestedBackdrop === 'next backdrop') { - stage.setCostume(stage.currentCostume + 1); - } else if (requestedBackdrop === 'previous backdrop') { - stage.setCostume(stage.currentCostume - 1); } else { const costumeIndex = stage.getCostumeIndexByName(requestedBackdrop.toString()); if (costumeIndex !== -1) { stage.setCostume(costumeIndex); + } else if (requestedBackdrop === 'next backdrop') { + stage.setCostume(stage.currentCostume + 1); + } else if (requestedBackdrop === 'previous backdrop') { + stage.setCostume(stage.currentCostume - 1); } else if (requestedBackdrop === 'random backdrop') { const numCostumes = stage.getCostumes().length; if (numCostumes > 1) { diff --git a/test/unit/blocks_looks.js b/test/unit/blocks_looks.js index ab0552a0..779f3610 100644 --- a/test/unit/blocks_looks.js +++ b/test/unit/blocks_looks.js @@ -129,9 +129,11 @@ test('switch backdrop block runs correctly', t => { t.strictEqual(testBackdrop(['a', 'b', 'c', 'd'], 'previous backdrop', 3), 2); t.strictEqual(testBackdrop(['a', 'b', 'c', 'd'], 'next backdrop', 2), 3); - // 'previous backdrop' and 'next backdrop' can't be overriden - t.strictEqual(testBackdrop(['a', 'previous backdrop', 'c', 'd'], 'previous backdrop', 4), 3); - t.strictEqual(testBackdrop(['next backdrop', 'b', 'c', 'd'], 'next backdrop'), 2); + // 'previous backdrop', 'previous backdrop', 'random backdrop' can be overriden + // Test is deterministic since 'random backdrop' will not pick the same backdrop as currently selected + t.strictEqual(testBackdrop(['a', 'previous backdrop', 'c', 'd'], 'previous backdrop', 4), 2); + t.strictEqual(testBackdrop(['next backdrop', 'b', 'c', 'd'], 'next backdrop', 3), 1); + t.strictEqual(testBackdrop(['random backdrop', 'b', 'c', 'd'], 'random backdrop'), 1); // NaN, Infinity, and true are the first costume t.strictEqual(testBackdrop(['a', 'b', 'c', 'd'], NaN, 2), 1);