在LUA .... Corona SDK ...我的新分数是重叠而没有删除旧分数?

时间:2013-11-10 04:15:51

标签: lua corona

在下面的代码中显示“SCORE:100”或其他任何因为得分/分数的变化 总数超过了另一个,你不能读它们...... 我希望在显示新的分数/分数之前删除/删除旧分数 任何想法如何修复这个...这是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

1 个答案:

答案 0 :(得分: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