From 05aa70baf3995e905250ed81d89938f9b4619ffa Mon Sep 17 00:00:00 2001 From: ryanleeb <1006076811@qq.com> Date: Fri, 22 Aug 2025 15:14:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81if=E7=9A=84=E7=A7=AF=E6=9C=A8?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/blocks/scratch3_control.js | 45 +++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/blocks/scratch3_control.js b/src/blocks/scratch3_control.js index bfededec968..7bd7149b3a3 100644 --- a/src/blocks/scratch3_control.js +++ b/src/blocks/scratch3_control.js @@ -38,7 +38,8 @@ class Scratch3ControlBlocks { control_get_counter: this.getCounter, control_incr_counter: this.incrCounter, control_clear_counter: this.clearCounter, - control_all_at_once: this.allAtOnce + control_all_at_once: this.allAtOnce, + control_if_elseif_else: this.ifElseIfElse, }; } @@ -201,6 +202,48 @@ class Scratch3ControlBlocks { // removed before the release of 2.0.) util.startBranch(1, false); } + + extractConditions (args) { + const conditions = {}; + Object.keys(args).forEach(key => { + const match = key.match(/^CONDITION(\d+)$/); // 匹配 IF + 数字 + if (match) { + const index = match[1] === '' ? 1 : parseInt(match[1], 10); + conditions[index] = Cast.toBoolean(args[key]); // 转换为布尔值 + } + }); + return conditions; + } + + ifElseIfElse(args, util){ + // 取else的索引 + var elseIdx = args.mutation && args.mutation.else; + // 主 if 条件 + const mainCondition = Cast.toBoolean(args.CONDITION); + if (mainCondition) { + util.startBranch(1, false); // 执行主 if 分支 + return; + } + // 提取所有 else if 条件 + const elseIfConditions = this.extractConditions(args); + const sortedIndices = Object + .keys(elseIfConditions) + .map(Number) + .sort(); + + // 按顺序检查 else if 条件 + for (let i = 0; i < sortedIndices.length; i++) { + const index = sortedIndices[i]; + if (elseIfConditions[index]) { + console.log(index, i+1); + util.startBranch(index, false); + return; + } + } + + // 如果没有 else if 成立,执行 else 分支 + util.startBranch(elseIdx, false); + } } module.exports = Scratch3ControlBlocks;