From 4419014421c40886c18c3738a0d0c27a55eeef5c Mon Sep 17 00:00:00 2001 From: Kenny2github Date: Sat, 5 May 2018 18:25:18 +0800 Subject: [PATCH 1/4] Handle random backdrop --- src/blocks/scratch3_looks.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/blocks/scratch3_looks.js b/src/blocks/scratch3_looks.js index 92500ac03..2f5a661e0 100644 --- a/src/blocks/scratch3_looks.js +++ b/src/blocks/scratch3_looks.js @@ -343,6 +343,8 @@ class Scratch3LooksBlocks { } else if (requestedCostume === 'next costume' || requestedCostume === 'next backdrop') { target.setCostume(target.currentCostume + 1); + } else if (requestedCostume === 'random backdrop') { + target.setCostume(Math.floor(Math.random() * target.getCostumes().length)); } else { const forcedNumber = Number(requestedCostume); if (!isNaN(forcedNumber)) { From e3b7f5331953ee7d47e96782ef5a5af52ad6eec1 Mon Sep 17 00:00:00 2001 From: Kenny2github Date: Tue, 8 May 2018 08:21:27 +0800 Subject: [PATCH 2/4] Make sure switched costume is not current costume --- src/blocks/scratch3_looks.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/blocks/scratch3_looks.js b/src/blocks/scratch3_looks.js index 2f5a661e0..3402b6047 100644 --- a/src/blocks/scratch3_looks.js +++ b/src/blocks/scratch3_looks.js @@ -344,7 +344,13 @@ class Scratch3LooksBlocks { requestedCostume === 'next backdrop') { target.setCostume(target.currentCostume + 1); } else if (requestedCostume === 'random backdrop') { - target.setCostume(Math.floor(Math.random() * target.getCostumes().length)); + if ((numCostumes = target.getCostumes().length) > 1) { + let index; + do { + index = Math.floor(Math.random() * numCostumes); + } while (index === target.currentCostume); + target.setCostume(index); + } } else { const forcedNumber = Number(requestedCostume); if (!isNaN(forcedNumber)) { From 3f1c4aa2d8a0b2bb611bbf6141087b15cce2421e Mon Sep 17 00:00:00 2001 From: Kenny2github Date: Tue, 8 May 2018 08:27:51 +0800 Subject: [PATCH 3/4] thought I was being smart by using inline assignment, but noooooo --- src/blocks/scratch3_looks.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/blocks/scratch3_looks.js b/src/blocks/scratch3_looks.js index 3402b6047..a2c9dbefb 100644 --- a/src/blocks/scratch3_looks.js +++ b/src/blocks/scratch3_looks.js @@ -344,7 +344,8 @@ class Scratch3LooksBlocks { requestedCostume === 'next backdrop') { target.setCostume(target.currentCostume + 1); } else if (requestedCostume === 'random backdrop') { - if ((numCostumes = target.getCostumes().length) > 1) { + const numCostumes = target.getCostumes().length; + if (numCostumes > 1) { let index; do { index = Math.floor(Math.random() * numCostumes); From 8e9f9e0a30d5da540e750f1aa33b647aa8e903fa Mon Sep 17 00:00:00 2001 From: Kenny2github Date: Wed, 9 May 2018 08:13:04 +0800 Subject: [PATCH 4/4] Add one to index if random costume is current costume --- src/blocks/scratch3_looks.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/blocks/scratch3_looks.js b/src/blocks/scratch3_looks.js index a2c9dbefb..310d9a3de 100644 --- a/src/blocks/scratch3_looks.js +++ b/src/blocks/scratch3_looks.js @@ -346,11 +346,9 @@ class Scratch3LooksBlocks { } else if (requestedCostume === 'random backdrop') { const numCostumes = target.getCostumes().length; if (numCostumes > 1) { - let index; - do { - index = Math.floor(Math.random() * numCostumes); - } while (index === target.currentCostume); - target.setCostume(index); + let selectedIndex = Math.floor(Math.random() * (numCostumes - 1)); + if (selectedIndex === target.currentCostume) selectedIndex += 1; + target.setCostume(selectedIndex); } } else { const forcedNumber = Number(requestedCostume);