Random 如何覆盖以前创建的newText对象
在这里,我创建了一个按钮,该按钮具有一个名为when button的函数Random 如何覆盖以前创建的newText对象,random,lua,coronasdk,Random,Lua,Coronasdk,在这里,我创建了一个按钮,该按钮具有一个名为when button的函数 local politeButton = widget.newButton { left = 35, top = 335, width = 70, height = 70, defaultFile = "images/politeRu.png", overFile = "images/politeWhiteRu.png", onPress = politeGenerator, } 这是函数(实际上它是在按钮上方编码的)
local politeButton = widget.newButton
{
left = 35,
top = 335,
width = 70,
height = 70,
defaultFile = "images/politeRu.png",
overFile = "images/politeWhiteRu.png",
onPress = politeGenerator,
}
这是函数(实际上它是在按钮上方编码的)
好的,现在我在模拟器中看到一些随机生成的文本。但当我再次按下按钮时,旧文本并没有消失,新文本出现在旧文本上,以此类推。但我需要每次按下按钮时都覆盖文本。我尝试了
event.phase==“开始”
,我尝试了赞美自己:removeSelf()
,但都是徒劳的。有人能帮忙吗?我只是不明白为什么我按下按钮时变量没有被覆盖。您可以使用更改文本。text
要更改旧文本,我用计时器重新编写了代码,以查看文本是否在更改,代码的问题在于调用生成器()
功能是始终初始化局部变量本身它不会覆盖现有文本,而是创建一个与旧文本重叠的新文本
local firstComplPart = {"I love", "I need", "I beg"}
local secondComplPart = {" you like mad ", " the color of your eyes ", " your lips "}
local thirdComplPart = {"and I wish you are going to be mine!", "and I am shivering!", "and this is all I want!"}
local politeCompliment = firstComplPart[firstRandomPart]..secondComplPart[secondRandomPart]..thirdComplPart[thirdRandomPart]
local complimentItself = display.newText(politeCompliment, 30, 150, 200, 200, "Lobster", 18)
local function listener()
politeCompliment = firstComplPart[math.random(1,3)]..secondComplPart[math.random(1,3)]..thirdComplPart[math.random(1,3)]
complimentItself.text = politeCompliment
end
timer.performWithDelay( 1000, listener, 0 )
我使用这个函数来创建和更新文本对象。很简单:
function createText( text, xPos, yPos, fontSize, color, refPoint )
local myText = display.newText( "", 0, 0, native.systemFont, fontSize )
myText.text = text
if refPoint == "CL" then
myText:setReferencePoint( display.CenterLeftReferencePoint )
elseif refPoint == "CR" then
myText:setReferencePoint( display.CenterRightReferencePoint )
elseif refPoint == "C" then
myText:setReferencePoint( display.CenterReferencePoint )
end
myText.x = xPos
myText.y = yPos
if color then myText:setTextColor( color[1], color[2], color[3] )
else myText:setTextColor(255, 255, 255) end
function myText:update( t, refPoint )
myText.text = t.text or myText.text
myText.size = t.fontSize or myText.size
if refPoint == "CL" then
myText:setReferencePoint( display.CenterLeftReferencePoint )
elseif refPoint == "CR" then
myText:setReferencePoint( display.CenterRightReferencePoint )
elseif refPoint == "C" then
myText:setReferencePoint( display.CenterReferencePoint )
end
myText.x = t.xPos or myText.x
myText.y = t.yPos or myText.y
end
return myText
end
例如:
local myText = createText( "Random text", 50, 160, 20, { 0, 0, 0 }, "C" )
myText:update( { text = "Updated random text", size = 30, xPos = 400, yPos = 300 }, "C" )
本身就是包含您刚才创建的文本的新小部件吗?如果是这样,当函数退出时,您将丢失对它的引用,并且以后无法删除它。因此,每次通过函数创建一个新的小部件,并且永远不要删除旧的小部件。你需要保留旧的参考资料,并将其从我想象的显示器上移除(尽管我对corona一无所知)。Etan,感谢你的参与,你的建议对我来说很有意义,但我仍然不知道“如何”或者我不明白我的错误到底出在哪里。你可以使用全局(或外部本地)存储旧引用并在再次运行生成程序时将其删除,或者了解如何连接其他机制(事件、信号等)以让文本小部件知道它需要删除自身。我不能说得更具体,因为我对科罗纳了解不够。全球/外部本地路线当然是最简单的。是的!这有帮助!非常感谢。在这里,我只需要把函数本身放在代码中正确的位置,一切都正常!谢谢你的关注,我将尝试一下你的功能,看看它是否符合我的一些需求!
local myText = createText( "Random text", 50, 160, 20, { 0, 0, 0 }, "C" )
myText:update( { text = "Updated random text", size = 30, xPos = 400, yPos = 300 }, "C" )