【问题标题】:Scoring overlay in Corona电晕中的得分叠加
【发布时间】:2013-08-18 19:29:09
【问题描述】:

我是 Corona 的新手,我的评分系统有点问题。看看会发生什么,当你开始游戏时,分数从 0 开始,这是应该的。当玩家获得分数时,它应该增加 2。好吧,它确实增加了它,而不是数字 0 变为数字 2,我得到的是数字 0,然后是 0 顶部的数字 2。所以它覆盖了。我找不到任何实际解决此问题的帖子,所以我认为我在这里做错了。有什么帮助吗?或者只是指出我正确的方向?预先感谢。 :)

【问题讨论】:

    标签: lua overlay coronasdk scoring


    【解决方案1】:

    试试这个并更改您的代码:

    score = 0
    local scoreText = display.newText(score, 100, 100, native.systemFont, 50)
    scoreText:setTextColor(255, 255, 255)
    
    function displayScore()
        --[[ The problem was here. You are creating new label over and over in 
              your code. So, you need to either remove the old label and add 
              new using 'scoreText:removeSelf()' or just update the code --]]
        score = score + 1
    scoreText.text = score
    end
    Runtime:addEventListener("tap",displayScore)
    

    继续编码............ :)

    【讨论】:

    • 非常感谢您的回答。 :) 我该如何更新代码?
    • 嗨,所以我喜欢你的代码,但问题是每次我“点击”屏幕时,它都会增加一个分数。我只希望它在用户在函数chopFruit 中斜线对象时添加分数。我该怎么做呢?
    • 我添加了最后一行以供您测试。注释该行并在需要更新分数时将函数调用为:'displayScore()'。
    【解决方案2】:

    您的代码的问题是每次调用 displayScore() 函数时,它都会创建另一个 newText,因为您总是调用

    local scoreText = display.newText("Score: ", 415, 100, native.systemFont, 50).

    尝试像这样在函数displayScore()之外声明scoreText

    local scoreText = display.newText("Score: ", 415, 100, native.systemFont, 50)
    
    function displayScore()
        scoreText:setTextColor(255, 255, 255)
        scoreText.text = scoreText.text = "Score: "..score
    
    end
    

    【讨论】:

    • 您好,感谢您的详细回答。现在你解释了,我真的明白你的意思了,但是当我尝试那个代码时,它根本没有在屏幕上显示任何东西。我认为问题可能出在这个: scoreText.text = scoreText.text = "Score: "..score 我一直在尝试搜索显示我的变量而不将其声明为 newText 但我找不到任何东西的代码。另外,由于某种原因打印不起作用?
    • 试着让你的 display.newText 的坐标代码像这样 local scoreText = display.newText("Score: ", 100, 100, native.systemFont, 50) 我认为问题是你不能看到它是因为 y 轴位置超出了您的屏幕尺寸
    • 不,我现在可以在我的屏幕上看到它。我尝试了另一种方法, function displayScore() scoreText.text = ("Score: " )..score score = score + 2 end timer.performWithDelay( 1000, displayScore) 现在的问题是,当玩家重新开始游戏时,水果的第一排,它仍然显示 0。你认为可能有什么问题?非常感谢你对我的耐心。 :)
    • 我现在修好了,我只是将 timer.performWithDelay(1000, displayScore) 放在 main() 中。
    • 你的问题解决了吗,如果其中一个解决了你的问题,你可以接受答案
    猜你喜欢
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多