还有第二种更复杂的方法可以做到这一点。它允许递归块,您可以多次运行一个块。我称它为堆叠方法,因为我使用列表作为堆栈。例如,请参阅我制作的这个项目。
此方法也不会弄乱变量调色板。
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
在渲染的 ScratchBlocks 中看到这一点。