1

在下面的代码中,它显示“SCORE:100”或其他任何内容,但是随着分数/分数的变化,总数重叠在另一个之上,您无法阅读它们......我希望在显示新分数之前删除/删除旧分数/points ANY THOUGHTS HOW TO FIX this...这是 LUA 并在我的测试期间使用 CORONA SDK 我已发送打印语句以尝试对部分进行故障排除

--积分正在另一个位置计算 --更新分数

local function updateScore(Points)

  if WTF == 1 then
    print ("SCORE: -->",Points)

    --PointsText:removeSelf(PointsText)

        PointsText = display.newText(Points,0,0,native.sytemFont,42)        
        PointsText.text = Points
        PointsText.xscale = 0.5; PointsText.yscale = 0.5
        PointsText:setTextColor(155,155,225)
        PointsText.x = centerX * 1
        PointsText.y = centerY - 150

        ScoreTxt = display.newText("Score: ",0,0,native.systemFont,40) 
        ScoreTxt:setTextColor(220,50,50)
        ScoreTxt.x = display.contentCenterX
        ScoreTxt.y = display.contentCenterY-100
    end
end
4

1 回答 1

3

每次调用 updateScore 时,您都在创建一个新的文本对象。此代码可确保您只创建一次文本。

local function updateScore(Points)
    if PointsText == nil then
        PointsText = display.newText(Points,0,0,native.sytemFont,42)        
    end

    PointsText.text = Points
    PointsText.xscale = 0.5; PointsText.yscale = 0.5
    PointsText:setTextColor(155,155,225)
    PointsText.x = centerX * 1
    PointsText.y = centerY - 150

    ScoreTxt = display.newText("Score: ",0,0,native.systemFont,40) 
    ScoreTxt:setTextColor(220,50,50)
    ScoreTxt.x = display.contentCenterX
    ScoreTxt.y = display.contentCenterY-100
end

你也可以这样做:

local function updateScore(Points)
    if PointsText then
        PointsText:removeSelf()     
    end

    PointsText = display.newText(Points,0,0,native.systemFont,42)        
    PointsText.text = Points
    PointsText.xscale = 0.5; PointsText.yscale = 0.5
    PointsText:setTextColor(155,155,225)
    PointsText.x = centerX * 1
    PointsText.y = centerY - 150

    ScoreTxt = display.newText("Score: ",0,0,native.systemFont,40) 
    ScoreTxt:setTextColor(220,50,50)
    ScoreTxt.x = display.contentCenterX
    ScoreTxt.y = display.contentCenterY-100
end
于 2013-11-10T04:38:10.967 回答