【问题标题】:Corona SDK. Lua. display.newText() - my updated score text is overlapping old one without erasing电晕SDK。卢阿。 display.newText() - 我更新的乐谱文本与旧的重叠,没有擦除
【发布时间】:2016-06-17 01:24:42
【问题描述】:

我正在使用 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(event) 我创建我们的文本

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

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

【问题讨论】:

  • 您是否会多次调用 scene:show(event) ?此外,对于更新文本的函数来说,eraseLines 是一个非常糟糕的名称。为什么不将其命名为 updateScoreLevel 或其他名称以避免混淆
  • @Piglet 1) 我只调用了一次 scene:show(event)。此事件有两个阶段:“将”和“已”。当我提出问题时,我的代码是在“意志”阶段执行的。然后我将它剪切并粘贴到“did”阶段,重叠不再发生。我不确定为什么它会这样工作。 2) 函数 eraseLines 会擦除屏幕上的俄罗斯方块块的行并更新文本。可能最好排除那里的文本操作表单并将其作为一个单独的函数。

标签: lua coronasdk


【解决方案1】:

此时您要添加两次分数/级别标签,因为 show 事件被调用两次(阶段)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

【讨论】:

    【解决方案2】:

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

        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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2013-08-22
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多