这里我正在创建一个按钮,其中包含按下按钮时调用的功能
local politeButton = widget.newButton
{
left = 35,
top = 335,
width = 70,
height = 70,
defaultFile = "images/politeRu.png",
overFile = "images/politeWhiteRu.png",
onPress = politeGenerator,
}
这是函数(实际上它是在按钮上面编码的)
math.randomseed(os.time())
local function politeGenerator()
local firstRandomPart = math.random(1,3)
local secondRandomPart = math.random(1,3)
local thirdRandomPart = math.random(1,3)
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)
end
好的,现在我在模拟器中看到一些随机生成的文本。但是,当我再次按下按钮时,旧文本不会消失,旧文本上会出现新文本,依此类推。但是每次按下按钮我都需要覆盖文本。我试过了event.phase == "began"
,我试过了complimentItself:removeSelf()
,但一切都是徒劳的。有人可以帮忙吗?我只是不明白为什么当按下按钮时变量不会被覆盖。
答案 0 :(得分:1)
您可以使用.text
来更改原始文本,我会使用计时器对代码进行编码,以查看文本是否正在发生变化,代码的问题是在您调用{{1}时函数是你总是初始化一个局部变量politeGenerator()
它不会覆盖现有的文本,而是创建一个与旧文本重叠的新文本
complimentItself
答案 1 :(得分: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" )