循环列表中两个对象之间的所有内容



我正在制作《Roblox》中的块编码引擎,我制作了一些块,如移动,改变大小和旋转。但是我不知道如何创建一个循环,我必须找到a for I,v in pairs循环中两项之间的每一项。

for i, block in pairs(script.Parent.Parent.Script:GetChildren()) do
wait(0.1)
if block.Value == "MoveX" then
game.Character.Position = UDim2.new(game.Character.Position.X.Scale, (game.Character.Position.X.Offset + block.EpicValue.Value), game.Character.Position.X.Scale, game.Character.Position.Y.Offset)
end
if block.Value == "MoveY" then
game.Character.Position = UDim2.new(game.Character.Position.X.Scale, game.Character.Position.X.Offset, game.Character.Position.X.Scale, (game.Character.Position.Y.Offset + block.EpicValue.Value))
end
if block.Value == "Rotate" then
game.Character.Rotation = game.Character.Rotation + block.EpicValue.Value
end
if block.Value == "SizeX" then
game.Character.Size = UDim2.new(game.Character.Size.X.Scale, math.abs(block.EpicValue.Value), game.Character.Size.X.Scale, game.Character.Size.Y.Offset)
end
if block.Value == "SizeY" then
game.Character.Size = UDim2.new(game.Character.Size.X.Scale, game.Character.Size.X.Offset, game.Character.Size.Y.Scale, math.abs(block.EpicValue.Value))
end

if block.Value == "LoopStart" then
-- start loop
end
end

使用while循环代替foreach循环。然后,您可以轻松地将这个while循环转换为状态机:

local i = 1
local instructions = script.Parent.Parent.Script:GetChildren()
while i < #instructions do
-- fetch current instruction
local block = instructions[i]
if block.Value == "MoveX" then
--...
elseif block.Value == "LoopStart" then
if not insertYourLoopCondition then
i = instructionIndexOfLoopEnd
end
elseif block.Value == "LoopEnd" then
i = instructionIndexOfLoopStart - 1
end
-- next instruction
i = i + 1
end

换句话说:你需要跳跃。一个循环由一个条件和"跳转"组成。最后回到状态。条件是一个"条件跳转"。当生成你的方块时,你需要一次收集所有跳跃的目标索引。