电晕SDK。 Lua。 display.newText() - 我更新的乐谱文本与旧乐谱重叠而没有擦除

Corona SDK. Lua. display.newText() - my updated score text is overlapping old one without erasing

我正在使用 Corona SDK 开发任天堂俄罗斯方块游戏的克隆版本。我的屏幕顶部有两个文本对象:一个代表当前级别,另一个代表当前分数。每次我用块填充行时,我的程序都会擦除此行并添加一些分数和 +1 级别。问题是,一旦我更新我的分数和级别变量并使用 myText.text 刷新我的文本,它不会删除旧文本并创建与旧文本重叠的新文本。

我的代码如下: 1)我在场景开始时声明了两个局部变量

local scoreText
local levelText

2) 我有擦除线条和更新文本的功能

function eraseLines()
            -- some code that erases lines
            scores = scores + 10
            scoreText.text = "Score:"..scores
            level = level + 1
            levelText.text = "Level:"..level
end

3) 在 scene:show(事件) 中我创建了我们的文本

function scene:show( event )
    -- some code    
    scoreText = display.newText("Score:"..scores, halfW*0.5, 20 )
    levelText = display.newText("Level:".. level, halfW*1.5, 20 )
    sceneGroup:insert( scoreText )
    sceneGroup:insert( levelText )
    scoreText:setFillColor( 0, 0, 0 )
    levelText:setFillColor( 0, 0, 0 )
end

请帮我找出重叠的原因

目前您正在添加两次 score/level 标签,因为演出事件被调用了两次(阶段)willdid。创建场景时添加显示对象。

-- create()
function scene:create( event )

    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen
    scoreText = display.newText( "Score: 0", halfW * 0.5, 20 )
    levelText = display.newText( "Level: 0", halfW * 1.5, 20 )
    sceneGroup:insert( scoreText )
    sceneGroup:insert( levelText )
    scoreText:setFillColor( 0, 0, 0 )
    levelText:setFillColor( 0, 0, 0 )

end


-- show()
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)
        scoreText.text = "Score: " .. score
        levelText.text = "Level: " .. level

    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen

    end
end

这是一个用于显示分数的构造不佳的单个脚本

    local scoreCounter = {}
    local frameTime = 0
    scoreCount = 0
    finalScore = nil
    local tempText = nil

    local function update( event )
         frameTime = frameTime + 1

         --after every 7 frames score will increase
         if(frameTime % 7 == 0) then
            scoreCount = scoreCount + 1
            tempText.text = scoreCount 
            frameTime = 0
         end
    end

    -- here is a memory leak I guess
    function scoreCounter.displayScore(xCoordinate, yCoordinate)
        tempText = display.newText(scoreCount, xCoordinate, yCoordinate)
    end

    Runtime:addEventListener("enterFrame", update)

    return scoreCounter

用法:

local scoreCounter = require("pathtoluafile")
scoreCounter.displayScore(xCoordinate, yCoordinate)