首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Coroutine问题

Coroutine问题
EN

Stack Overflow用户
提问于 2016-05-28 19:53:11
回答 1查看 329关注 0票数 1

因此,我试图在ROBLOX中制作一个基本的GUI动画系统,使用单个框架和一个循环将它们放入图像标记中。

这是一项功能:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
20:41:01.934 - Players.Player1.PlayerGui.ScreenGui.Gui-MAIN:65: bad argument #3 to 'Position' (UDim2 expected, got number)

尽管这个错误似乎没有起到任何作用。(例如,完全停止脚本)

导致错误的行被标记为注释。

我已经确认了pos是正确的。我甚至尝试在设置它之前打印它,它打印正确的东西:{0,120},{0,65}

另一个主要的问题是我不能在使用它一次之后继续使用它。它可以多次很好地运行这一行:

代码语言:javascript
复制
coroutine.resume(playAnimation,"Cannon Fire",UDim2.new(0,120,0,68-25),tank.Frame)

但它不能运行:

代码语言:javascript
复制
if tank2:FindFirstChild("Ammo") and isTouching(ammoFrame,tank2:GetChildren()[3]) then
    local lastAmmoPos = ammoFrame.Position
    ammoFrame:Destroy()
    coroutine.resume(playAnimation,"Explosion",lastAmmoPos-UDim2.new(0,25-(ammoTypes[type]["Size"].X.Offset)/2,0,25),tank.Frame)
    tank2:GetChildren()[3]:Destroy()
end

是的,如果语句运行良好。ammoFrame被摧毁了,tank2的第三个孩子也被摧毁了。合作线就不会恢复了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-29 15:24:20

通过完全删除coroutine并将for循环封装在一个产卵函数中进行修正。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37503233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档