在Lua中定义函数时出错,Corona SDK。

请帮我找出代码中的问题。

我正在定义一个函数:

local function goOnLesson()
   if date.hour==1  then
   index=1
   local subj=schToday[index]
   local text = display.newRetinaText("А сейчас у тебя: "..subj, 0, 0, native.systemFont, 70)
   text:setTextColor(128,64,0)
   text:setReferencePoint(display.CenterReferencePoint)
   localGroup:insert(text)
   end
end

当我运行它时,一切都正常。

我重新组织了代码,不再需要 if 块:

local function goOnLesson()
   index=1
   local subj=schToday[index]
   local text = display.newRetinaText("А сейчас у тебя: "..subj, 0, 0, native.systemFont, 70)
   text:setTextColor(128,64,0)
   text:setReferencePoint(display.CenterReferencePoint)
   localGroup:insert(text)
end

我只是删除了 if-end 块,但现在它不起作用了。 请帮忙 :)

点赞
用户1442917
用户1442917

这个错误似乎是一个通用消息,当在 Corona 事件执行期间出现运行时错误时会出现。我发现有几个关于相同问题的参考(例如这里)。

可能是 schToday 没有任何元素,你的 subj 变量得到一个 nil 值,然后在字符串拼接失败。你的原始代码可能“正常工作”,因为它不进入那个部分(你的 day.hour == 1 条件大部分时间返回“false”)。

我建议你仔细检查你的代码,确保它按照你的预期工作。

另一个要尝试的事情是局部化你的 index 变量。可能是你给它赋了一个值,在你程序的其他部分破坏了一些东西。

2012-09-03 23:12:38