协同问题

时间:2016-05-28 19:53:12

标签: lua coroutine roblox

所以我试图在ROBLOX中制作一个基本的GUI动画系统,使用单独的帧和一个循环将它们放入图像标签。

这是功能:

@import "../pages/yourpage/yourpage"

这有两个主要问题: 每次运行时,我都会收到此错误:

local playAnimation = coroutine.create(function(anim,pos,tank)
    while true do
    local animBase = sp.AnimBase:Clone()
    animBase.Parent = tank
    animBase.Visible = true
    animBase.Position = pos -- line that causes the error mentioned below.
    local frame = 1
    for i = 0, animations[anim]["FrameNum"] do
        frame = frame + 1
        animBase.Image = animations[anim]["Frames"][frame]
        NewWait(.1) --this right here, the wait, interfears with the yield.
        if frame >= animations[anim]["FrameNum"] then
            pos,anim,tank = coroutine.yield()
            break
        end
    end
    animBase:Destroy()
    end
end)

虽然这个错误似乎没有做任何事情。 (例如,完全停止脚本)

导致错误的行标有注释。

我确定pos是正确的。我甚至在设置之前尝试打印它,它打印正确的东西是: {0120},{0,65}

另一个主要问题是我在使用一次后无法恢复它。它可以多次运行此行:

20:41:01.934 - Players.Player1.PlayerGui.ScreenGui.Gui-MAIN:65: bad argument #3 to 'Position' (UDim2 expected, got number)

但它不会运行:

coroutine.resume(playAnimation,"Cannon Fire",UDim2.new(0,120,0,68-25),tank.Frame)

是的,if语句工作正常。 ammoFrame被破坏,因此是tank2的第三个孩子。协程刚刚恢复了。

1 个答案:

答案 0 :(得分:0)

通过完全删除协程并将for循环包装在spawn函数中来修复。

local playAnimation = function(anim,pos,tank)
    local animBase = sp.AnimBase:Clone()
    animBase.Parent = tank
    animBase.Visible = true
    animBase.Position = pos
    local frame = 1
    spawn(function()
        for i = 0, animations[anim]["FrameNum"] do
            frame = frame + 1
            animBase.Image = animations[anim]["Frames"][frame]
            wait(.1) --this right here, the wait, interfears with the yield.
            if frame >= animations[anim]["FrameNum"] then
                break
            end
        end
        animBase:Destroy()
    end)
end