如何在单击提交后隐藏 native.newTextField?

我正在努力让用户的名字每个字母依次以垂直方式淡入。例如:Adam的“A”会在1秒钟后出现,“d”会在显示的“A”下面的3秒钟后出现,“a”会在显示的“d”下面的5秒钟后出现,“m”会在显示的“a”下面的7秒钟后出现。视觉效果会有一种多米诺骨牌效应。当它们出现时,它们将停留在屏幕上显示。

当我注释掉userNameField:removeSelf()时,代码就可以正常工作。我得到了想要的效果,但问题是我仍然有userNamefield显示出来。

如果需要更多代码,请告诉我。

local greeting = display.newText( "Greetings, enter your name",0,0,native.systemFont, 20 )
greeting.x = display.contentWidth/2
greeting.y = 100

local submitButton = display.newImage( "submit.png"  ,display.contentWidth/2, display.contentHeight - 50 )

local userNameField = native.newTextField( display.contentWidth * 0.5 , 150, 250, 45)
userNameField.inputtype = "defualt"

local incrementor = 0
function showNameLetters()

userNameField:removeSelf( )
if incrementor <= string.len ("userNameField.text") then
    incrementor = incrementor + 1
    personLetters = string.sub (userNameField.text, incrementor,incrementor)
    display_personLetters = display.newText (personLetters, 290,30*incrementor, native.systemFont, 30)
        display_personLetters.alpha = 0
    transition.to(display_personLetters,{time = 3000, alpha = 1, onComplete = showNameLetters})
end
end

更新:

我已经找到了解决我的问题的方法,即在我的函数中添加userNameField.isVisible = false。

我还发现了一些非常奇怪的事情,希望有人能解释为什么会发生这种情况。如果我添加了greeting:removeSelf()和submitButton:removeSelf()(我已经在下面的代码中注释掉它们以展示我在测试中放置它们的位置),我会得到只淡入第一个字母的奇怪结果。如果我将 greeting.isVisible = false 和 submitButton.isVisible = false。那么代码就可以正常工作。

我很困惑为什么object:removeSelf()不起作用。有人能为我解决这个问题吗?

换句话说,如果我用以下线替换:

userNameField:removeSelf()。

userNameField.isVisible = false

然后该应用程序就可以正常工作。请为我建议为什么/任何解决方案。提前致谢...

点赞
用户2054602
用户2054602

似乎你正在多次调用 showNameLetters,这意味着你要移除本地的文本字段不止一次。将其置为 nil 并在移除之前进行 nil 检查,像这样:

if userNameField ~= nil then
 userNameField:removeSelf()
 userNameField = nil
end
2014-03-29 20:50:04
用户1979583
用户1979583

以下是中文翻译,保留原始的 markdown 格式:

这段代码表明你在删除 userNameField 之后还在使用它,如下所示:

personLetters = string.sub (userNameField.text, incrementor,incrementor)

你也在反复调用 object:removeSelf(),但是没有检查它是否存在(正如 hades2510 所述)。因此,在删除 userNameField 之前,要检查其是否存在:

if userNameField ~= nil then
 userNameField:removeSelf()
 userNameField = nil
end

userNameField 为 nil 的时候,你将无法获取 userNameField.text。因此,请使用一个临时变量来保存以前的 userNameField.text,在需要时从该变量获取保存的数据。

额外提示: 你确定在下面的代码行中要检查文本 "userNameField.text" 的长度,而不是要使用 userNameField.text 变量的长度吗?如果你需要使用文本框中的数据,那么这也很重要。

if incrementor <= string.len ("userNameField.text") then
  ........
end

继续编程…… :)

2014-04-03 12:31:04