在 Scratch 中模拟自定义报告块?
Simulating a custom reporter block in Scratch?
在 Scratch 2.0 中,添加了对自定义堆栈块 (procedures) 的支持。但是有什么方法可以将此用于 "abstract away" 逻辑,即 returns 一个值?
例如,我这里有一个简单计算指数的脚本:(view graphic representation)
set [base v] to [2]
set [index v] to [3]
... // above is for initializing
set [result v] to (base)
repeat until <(index) = [1]>
set [result v] to ((result) * (base))
change [index v] by (-1)
如何将此逻辑导出到 "custom reporter" 以供重用?
最简单的方法是创建自定义命令方块,并将 return 值存储在变量中。这有一些缺点,例如不允许递归调用,但在大多数情况下都有效。我还建议在不刷新屏幕的情况下将块设置为 运行。
像这样简单地定义它,return 值可用 result
:
define (base) ^ (exp)
set [index v] to (exp) // need a variable, as arguments are immutable
set [result v] to (base)
repeat until <(index) = [1]>
set [result v] to ((result) * (base))
change [index v] by (-1)
然后可以这样调用:
when gf clicked
(4) ^ (3) // the stack block
say (join [4 ^ 3 = ] (result)) // result is set by the [()^()] block
还有第二种更复杂的方法。它允许递归块,并且您可以多次 运行 一个块。我称它为堆叠方法,因为我使用列表作为堆栈。有关示例,请参阅 this project that i made.
这种方法也不会弄乱变量面板。
define (base) ^ (index) recursive
if <(index) = [1]>
add (base) to [stack v]
else
(base) ^ ((index) - (1)) recursive // adds the previous item to the stack
add ((base) * (item (last v) of [stack v])) to [stack v]
delete ((length of [stack v]) - (1)) of [stack v] // clean up
然后可以使用与我在 中解释的基本相同的方法访问它:
when gf clicked
(4) ^ (3) recursive // the stack block
say (join [4 ^ 3 = ] (item (last v) of [stack v])) // get the item from the end of the stack
delete [last v] of [stack v] // optional, if you want to clean up
这是一个例子 (rendered):
define split [text] by [splitter]
delete (all v) of [output list v]
set [parse v] to [0]
set [cur string v] to []
repeat until ((parse) > (length of (splitter))
if <(letter (parse) of (text)) = (splitter)> then
add (cur string) to [output list v]
set [cur string v] to []
else
set [cur string v] to (join (cur string) (letter (parse) of (text)))
end
end
when GF clicked
split [Hello, world! Do you like this?] by [ ] // That's a space.
// That should output a list: ["Hello,", "world!", "Do", "you", "like", "this?"]
define the answer to life
set [output var v] to (42)
when GF clicked
the answer to life
say (output var)
它展示了如何使用列表输出和变量输出。
在 Scratch 2.0 中,添加了对自定义堆栈块 (procedures) 的支持。但是有什么方法可以将此用于 "abstract away" 逻辑,即 returns 一个值?
例如,我这里有一个简单计算指数的脚本:(view graphic representation)
set [base v] to [2]
set [index v] to [3]
... // above is for initializing
set [result v] to (base)
repeat until <(index) = [1]>
set [result v] to ((result) * (base))
change [index v] by (-1)
如何将此逻辑导出到 "custom reporter" 以供重用?
最简单的方法是创建自定义命令方块,并将 return 值存储在变量中。这有一些缺点,例如不允许递归调用,但在大多数情况下都有效。我还建议在不刷新屏幕的情况下将块设置为 运行。
像这样简单地定义它,return 值可用 result
:
define (base) ^ (exp)
set [index v] to (exp) // need a variable, as arguments are immutable
set [result v] to (base)
repeat until <(index) = [1]>
set [result v] to ((result) * (base))
change [index v] by (-1)
然后可以这样调用:
when gf clicked
(4) ^ (3) // the stack block
say (join [4 ^ 3 = ] (result)) // result is set by the [()^()] block
还有第二种更复杂的方法。它允许递归块,并且您可以多次 运行 一个块。我称它为堆叠方法,因为我使用列表作为堆栈。有关示例,请参阅 this project that i made.
这种方法也不会弄乱变量面板。
define (base) ^ (index) recursive
if <(index) = [1]>
add (base) to [stack v]
else
(base) ^ ((index) - (1)) recursive // adds the previous item to the stack
add ((base) * (item (last v) of [stack v])) to [stack v]
delete ((length of [stack v]) - (1)) of [stack v] // clean up
然后可以使用与我在
when gf clicked
(4) ^ (3) recursive // the stack block
say (join [4 ^ 3 = ] (item (last v) of [stack v])) // get the item from the end of the stack
delete [last v] of [stack v] // optional, if you want to clean up
这是一个例子 (rendered):
define split [text] by [splitter]
delete (all v) of [output list v]
set [parse v] to [0]
set [cur string v] to []
repeat until ((parse) > (length of (splitter))
if <(letter (parse) of (text)) = (splitter)> then
add (cur string) to [output list v]
set [cur string v] to []
else
set [cur string v] to (join (cur string) (letter (parse) of (text)))
end
end
when GF clicked
split [Hello, world! Do you like this?] by [ ] // That's a space.
// That should output a list: ["Hello,", "world!", "Do", "you", "like", "this?"]
define the answer to life
set [output var v] to (42)
when GF clicked
the answer to life
say (output var)
它展示了如何使用列表输出和变量输出。