Lua 字符串兼容性错误,尝试索引nil值
我正在使用Gideros,出现以下错误:Lua 字符串兼容性错误,尝试索引nil值,lua,gideros,Lua,Gideros,我正在使用Gideros,出现以下错误: main.lua:47: attempt to index a nil value stack traceback: main.lua:47: in function 'func' [string "compatibility.lua"]:36: in function <[string "compatibility.lua"]:35> 索引nil错误意味着在该行中,您可能会从LooserText:getParent
main.lua:47: attempt to index a nil value
stack traceback:
main.lua:47: in function 'func'
[string "compatibility.lua"]:36: in function <[string "compatibility.lua"]:35>
索引nil
错误意味着在该行中,您可能会从LooserText:getParent()
获得nil
作为返回值
为什么你会得到
nil
,我不能告诉你,大概是因为它没有。文档表明Stage.addChild
没有错误条件,只是添加的对象必须是精灵TextField
继承了Sprite,因此您没有明显的理由出现此错误。但是,不应将delayedCall
的返回值重新分配给与Timer
类同名的全局变量,这可能会影响应用程序的其他部分。由于您没有使用返回的计时器
实例,因此我已删除了分配。此外,如果stage:addChild
成功,则删除
可以使用stage
。奇怪的是,您的oneter
只定义了youLose()
,但没有调用或返回它,这是您编写的代码的一部分吗?在任何情况下,您都需要添加一些健全性检查,以验证您认为正在发生的事情是否确实正在发生,w/r/t child add/remove:
function onEnter()
function youLoose()
local font2 = TTFont.new("billo.ttf", 20, "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
LoserText = TextField.new(font2, "You Lose , Try AGAIN?")
LoserText:setPosition(100, 100)
print('Stage num children:' .. stage:getNumChildren())
stage:addChild(LoserText)
print('Stage num children:' .. stage:getNumChildren())
print('LoserText is stage child #' .. stage:getChildIndex(LoserText))
Timer.delayedCall(1000, removing)
end
end
function removing()
print('Stage num children:' .. stage:getNumChildren())
print('LoserText is stage child #' .. stage:getChildIndex(LoserText))
stage:removeChild(LoserText)
print('Stage num children:' .. stage:getNumChildren())
end
这不是完整的错误消息,只是位置。错误信息是什么?同时,你“输”了一场比赛/比赛,当它不合适时,有些东西“松了”。另外,您可能不希望覆盖
Timer
,假设该调用返回的值不是Timer
模块,并且Timer
是模块而不是计时器的实例。需要知道代码中的第32行是什么;需要完整信息并在错误处标记行occurs@ColonelThirtyTwo:已更新错误message@Schollii:已标记,请检查?我想,它应该与stage:removeChild(LooserText)一起工作,但显然它不工作。如果我将文本添加到stage,那么我肯定可以从那里删除它,在这种情况下,它说如果我想把它从舞台上移除,它应该是舞台的孩子,你知道吗?stage:removeChild(LooserText)
?我对吉德罗斯一无所知,但我也认为这应该行得通。显然,它没有。错误消息说LooserText应该是stage的子级,如果我将它添加到stage,它肯定是一个根本不起作用的子级。您是否在代码中的其他地方使用LooserText
做其他事情?如果您在调用stage:addChild(LooserText)之前检查该阶段的子级数量
然后它会再次改变吗?
function onEnter()
function youLoose()
local font2 = TTFont.new("billo.ttf", 20, "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
LoserText = TextField.new(font2, "You Lose , Try AGAIN?")
LoserText:setPosition(100, 100)
print('Stage num children:' .. stage:getNumChildren())
stage:addChild(LoserText)
print('Stage num children:' .. stage:getNumChildren())
print('LoserText is stage child #' .. stage:getChildIndex(LoserText))
Timer.delayedCall(1000, removing)
end
end
function removing()
print('Stage num children:' .. stage:getNumChildren())
print('LoserText is stage child #' .. stage:getChildIndex(LoserText))
stage:removeChild(LoserText)
print('Stage num children:' .. stage:getNumChildren())
end