Re: Blocks Issue

47 views
Skip to first unread message

Neil Fraser

unread,
Nov 23, 2022, 5:59:29 AM11/23/22
to blo...@googlegroups.com
There are two approaches to executing code block by block, rather than at full speed.  The most powerful and flexible approach is to use the JS-Interpreter.  However its interface to your API isn't the simplest thing to configure.  The other approach is to record a log, then play it back slowly.  Let's look at this second approach.

Your blocks generate code that looks like this:

moveUp();
moveLeft();

Next write functions that simply log these movements, don't actually perform animations:

var log = [];
function moveUp() {
  log.push('UP');
}
function moveLeft() {
  log.push('LEFT');
}

Once the user's program is fully executed, then step through the log, with a delay between each step:

function step() {
  var action = log.shift();
  if (!action) return;
  if (action === 'UP') {
    avatar.style.left = (parseInt(avatar.style.left) - 10) + 'px';
  } else if (action === 'LEFT')
    avatar.style.left = (parseInt(avatar.style.top) - 10) + 'px';
  }
  // Wait 1000 ms, then execute the next step.
  setTimeout(step, 1000);
}
step();

Am Mo., 21. Nov. 2022 um 23:53 Uhr schrieb Lol Lol <loll...@gmail.com>:
I am working on blockly but the problem I am facing is that I use the var code to run a function and the problem is that when I have multiple blocks connected, they run at the same time so when wanting my image to go up and then left but it just goes diagonal. It might just be because I am a beginner at programming but is there a way for the blocks to go one at a time without like a delay?  

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/435b809f-b1a8-482b-947b-6bd7912b7c6en%40googlegroups.com.


--
Neil Fraser, Switzerland
https://neil.fraser.name
Reply all
Reply to author
Forward
0 new messages