Update repeat implementation to execute once per frame

This commit is contained in:
Tim Mickel 2016-07-01 10:50:31 -04:00
parent de6ba08866
commit 39c71b559d

View file

@ -28,15 +28,27 @@ Scratch3ControlBlocks.prototype.repeat = function(args, util) {
if (util.stackFrame.loopCounter === undefined) { if (util.stackFrame.loopCounter === undefined) {
util.stackFrame.loopCounter = parseInt(args.TIMES); util.stackFrame.loopCounter = parseInt(args.TIMES);
} }
// 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 // Decrease counter
util.stackFrame.loopCounter--; util.stackFrame.loopCounter--;
// If we still have some left, start the substack // If we still have some left, start the substack
if (util.stackFrame.loopCounter >= 0) { if (util.stackFrame.loopCounter >= 0) {
util.startSubstack(); util.startSubstack();
} }
} else {
util.stackFrame.executed = false;
util.yieldFrame();
}
}; };
Scratch3ControlBlocks.prototype.forever = function(args, util) { 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) { if (!util.stackFrame.executed) {
util.stackFrame.executed = true; util.stackFrame.executed = true;
util.startSubstack(); util.startSubstack();