From 39c71b559d5da903159d17b8c6f670d3bd252a8d Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Fri, 1 Jul 2016 10:50:31 -0400 Subject: [PATCH] Update repeat implementation to execute once per frame --- src/blocks/scratch3_control.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/blocks/scratch3_control.js b/src/blocks/scratch3_control.js index 161934ea4..d938f66ab 100644 --- a/src/blocks/scratch3_control.js +++ b/src/blocks/scratch3_control.js @@ -28,15 +28,27 @@ Scratch3ControlBlocks.prototype.repeat = function(args, util) { if (util.stackFrame.loopCounter === undefined) { util.stackFrame.loopCounter = parseInt(args.TIMES); } - // Decrease counter - util.stackFrame.loopCounter--; - // If we still have some left, start the substack - if (util.stackFrame.loopCounter >= 0) { - util.startSubstack(); + // Only execute once per frame. + // When the substack finishes, `repeat` will be executed again and + // the second branch will be taken, yielding for the rest of the frame. + if (!util.stackFrame.executed) { + util.stackFrame.executed = true; + // Decrease counter + util.stackFrame.loopCounter--; + // If we still have some left, start the substack + if (util.stackFrame.loopCounter >= 0) { + util.startSubstack(); + } + } else { + util.stackFrame.executed = false; + util.yieldFrame(); } }; Scratch3ControlBlocks.prototype.forever = function(args, util) { + // Only execute once per frame. + // When the substack finishes, `forever` will be executed again and + // the second branch will be taken, yielding for the rest of the frame. if (!util.stackFrame.executed) { util.stackFrame.executed = true; util.startSubstack();