【问题标题】:Display score using spritesheet使用 spritesheet 显示分数
【发布时间】:2015-02-25 13:01:26
【问题描述】:

我想知道如何使用 spritesheet 显示分数。我的游戏是关于积分收集的,我想用这个能量棒来填满。当能量条已满时,会弹出一个空的能量条,满的能量条将在游戏结束时消失。我拥有的 spritesheet 包含 70 张 png 图像。

我可以使用 if 语句来构建它,但必须有更好的方法。否则它看起来像这样

if score == 0 then
    display.newImage("00.png", x, y)
end
if score == 1 then
    display.newImage("01.png", x, y)
end
if score == 2 then
    display.newImage("02.png", x, y)
end
if score == 3 then
    display.newImage("03.png", x, y)
end
...
if score == 70 then
    display.newImage("70.png", x, y)
end

当分数为 71 时显示“01.png”

【问题讨论】:

标签: lua coronasdk sprite sprite-sheet


【解决方案1】:

由于分数值和您使用的文件名之间似乎存在直接关系(意味着 00 -> '00.png', 1 -> '01.png', ... 70 -> '70.png ' 等),并且在 score=70 之后,整个序列重复,这样做的一种方法是首先去掉 70 的倍数,然后在前面附加 0 以获得单个数字分数。这是一个可以做到这一点的函数:

-- Given a score, returns correct picture name
-- eg. for score = 01 returns 01.png
local function getFilenameFromScore(score)
    while true do
        if score < 71 then break end

        -- get rid of multiplies of 70 by reducing score by 70
        -- until it's 0-70
        score = score - 70
    end

    -- if score is between 0 and 9 (one digit, so length is 1)
    -- add 0 in front
    -- this could also be done with modulo %
    if string.len(score) == 1 then
        score = '0' .. score
    end

    -- append .png and return
    return score .. '.png'
end

然后,显示分数如下:

local scorePicture = getFilenameFromScore(score)

display.newImage(scorePicture, x, y)

这里,scorePicture 将取决于您描述的分数值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-18
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2013-02-27
    • 1970-01-01
    相关资源
    最近更新 更多