Corona SDK。 Lua中。 display.newText() - 我更新的分数文本与旧的重叠文本没有删除

时间:2016-06-17 01:24:42

标签: lua corona

我正在使用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)在场景中: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

请帮我找出重叠发生的原因

2 个答案:

答案 0 :(得分:2)

当您添加两次得分/等级标签时,'因为展示事件被调用两次(阶段)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

答案 1 :(得分:0)

这是一个显示得分不佳的单一脚本

    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)