如何覆盖先前创建的newText对象?

在这里,我创建了一个按钮,按下按钮时调用一个名为 "politeGenerator" 的函数。

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 = {"我爱你", "我需要你", "我恳求你"}
local secondComplPart = {"像疯狂一样爱你", "你的眼睛颜色", "你的嘴唇"}
local thirdComplPart = {"我希望你会成为我的!", "我在发抖!", "这是我想要的全部!"}

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(),但都无济于事。有谁能帮忙吗?我只是不明白为什么按下按钮时变量不会被覆盖。

点赞
用户1682268
用户1682268

你可以使用.text来更改旧文本。我用一个定时器重新编码了你的代码,以便查看文本是否正在更改。你的代码问题在于,当你调用politeGenerator()函数时,你总是初始化一个局部变量complimentItself,它不会覆盖现有的文本,而是创建一个新文本,重叠旧文本。

local firstComplPart = {"我爱", "我需要", "我乞求"}
local secondComplPart = {"你像疯子一样", "你的眼睛颜色", "你的嘴唇"}
local thirdComplPart = {"而且我希望你会是我的!", "我发抖了!", "这就是我想要的全部!"}

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 )
2013-07-31 00:38:47
用户2186639
用户2186639

我正在使用这个函数创建和更新文本对象。很简单:

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" )
2013-07-31 10:01:20