Skip to content

Mapping between Scratch Block and ScratchFoot generated Code

VictorNorman edited this page Jul 7, 2016 · 13 revisions

Scratch block and corresponding generated Java code.

Event blocks

In the constructor whenFlagClicked("whenFlagClickedCb0"); This function will also be generated in the Sprite:

public void whenFlagClickedCb0(Sequence s) {
    // Code here from the block.
}

In the constructor whenSpriteClicked("whenSpriteClickedCb0"); This function will also be generated in the Sprite:

public void whenSpriteClickedCb0(Sequence s) {
    // Code here from the block.
}

In the constructor whenKeyPressed("KeyName", "whenKeyNamePressedCb0"); where KeyName is the Greenfoot key name (e.g., "Up" for the up arrow.) This function will also be generated in the Sprite:

public void whenKeyNamePressedCb0(Sequence s) {
    // Code here from the block.
}

In the constructor whenRecvMessage("Message", "whenIReceiveMessageCb0"); where Message is the message used in Scratch. This function will also be generated in the Sprite:

public void whenIReceiveMessageCb0(Sequence s) {
    // Code here from the block.
}

In the constructor whenIStartAsAClone("whenIStartAsACloneCb0");

This function will also be generated in the Sprite:

public void whenIStartAsACloneCb0(Sequence s) {
    // Code here from the block.
}

Control blocks

wait(s, 1);

Always pass the parameters s. The second parameter is the time in seconds, and maybe a floating point number, e.g., 3.5.

Note that when you use this, the number Speed specified in the Greenfoot GUI will be ignored. That is, the call will not return until the number of seconds has passed, regardless of the execution Speed.


while (true)      // forever loop
{
    // forever loop block generated here
    yield(s);     // allow other sequences to run
}

Always leave the yield(s); call in the loop. If this is removed, no other scripts will run.


for (int i = 0; i < VALUE; i++) 
{
    // block code generated here
    yield(s);     // allow other sequences to run 
}

Always leave the yield(s); call in the loop. If this is removed, no other scripts will run.

If you use nested repeat blocks, you will need to replace the index variable name i with another variable name.


if (BOOLEAN EXPRESSION) 
{
    // block code generated here
}

if (BOOLEAN EXPRESSION) 
{
    // block code generated here
} 
else 
{
    // block code generated here
}

while (true) {
    if (BOOLEAN EXPRESSION)
        break;
    yield(s);
}

Always leave the yield(s); call in the loop. If this is removed, no other scripts will run.


while (! BOOLEAN EXPRESSION) {
    // block code generated here
    yield(s);
}

Always leave the yield(s); call in the loop. If this is removed, no other scripts will run.


stopAll(); or stopThisScript(); or stopOtherScriptsInSprite(); depending on the choice.


createCloneOfMyself(); or createCloneOf("SpriteName"); depending on the choice.


deleteThisClone();