Corona SDK,如何在排行榜中保存字符串?

时间:2017-01-14 15:40:12

标签: arrays variables lua corona lua-table

我的排行榜存在问题,问题是我的数组得分都按升序排序并且工作正常,但我无法在分数旁边保存文字?情况是game.lua - > gameOver(得分和难度文本) - >排行榜(分数)。从表中调用变量并不起作用。

leaderboards.lua

for i = 1, 10 do
    if (scoresTable[i]) then
      local yPos = 150 + (i * 130)

      local thisScore = display.newText(sceneGroup, scoresTable[i].. options.title,display.contentCenterX-30, yPos, font, 100)
      thisScore.anchorX = 0

    end
  end

game.lua

options{
  title = "Easy",
}

gameover.lua

options{
   title = options.title,
}

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您的leaderboard是只有那样的分数的表

leaderboard = { 100, 200, 300 }

并且您还希望将得分中的字符串(难度级别)放入其中。可以这样做

leaderboard = { {100, "Easy"}, {200, "Hard"}, {300, "Easy"} }

访问leaderboard

的元素
leaderboard[1]     -> {100, "Easy"}
leaderboard[1][1]  -> 100
leaderboard[1][2]  -> "Easy"

排序表

function compare(a,b)
  return a[1] < b[1]
end

table.sort(leaderboard, compare)