0

在这里,我正在创建一个按钮,该按钮具有按下按钮时调用的函数

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()但都是徒劳的。有人可以帮忙吗?我只是不明白为什么当我按下按钮时变量没有被覆盖。

4

2 回答 2

1

您可以通过使用更改.text旧文本来更改 文本现有文本,而是创建一个与旧文本重叠的新文本politeGenerator()complimentItself

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 )
于 2013-07-31T00:38:47.470 回答
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" )
于 2013-07-31T10:01:20.360 回答