【问题标题】:In Corona SDK how can I put a score keeper into my app?在 Corona SDK 中,我如何将记分器放入我的应用程序中?
【发布时间】:2014-09-15 01:53:03
【问题描述】:

我希望我正在制作的游戏在每次球击中球拍时为得分增加 1 分。(它是一个乒乓球游戏)我还希望游戏能够保存高分。这是我尚未制作的游戏中唯一的元素。如果有人可以提供帮助,那就太好了。

【问题讨论】:

    标签: android ios cross-platform coronasdk


    【解决方案1】:

    你可以用更简单的方式解决你的问题..

    只需像这样为分数声明一个变量..

    local score=0

    然后每当它击中桨时,将 score 变量增加 1。所以在 Collision Function 中插入如下代码:

    local function onCollision(event)
    {
    score=score+1
    }
    ball.collision=onCollision
    ball:addEventListener("collision",ball)
    

    最后,当你需要保存你的高分时(游戏结束后),你可以使用 Preference 而不是 json,这会产生更大的编码。

    local preference= require "preference"
    local highscore=0
    
    preference.save{highscore=score}
    

    如果要显示高分,请使用以下内容:

    highscore_value=preference.getValue("highscore")
    display.newText(highscore_value,0,0,nil,30)
    

    这可能对您的问题有用!

    【讨论】:

      【解决方案2】:

      您可以将高分保存到 json 文件中。

      这是一个演示:

      local json = require"json"
      
      ----------------------------------------------------------------
      local M = {} --- useful functions
      function M.load_json_from_file(ffn)
          if ffn == nil then return nil end
          local fhd = io.open(ffn, "rb")
          if fhd ~= nil then
              local contents = fhd:read("*a")
              fhd:close()
              return M.formatTable(json.decode(contents))
          else
              return nil
          end
      end
      
      function M.save_json_to_file(filepath, _table)
          local fhd = io.open(filepath, "wb")
          if fhd == nil then return false end
          local string4save = json.encode(_table)
          fhd:write(string4save)
          fhd:write("\r\n")
          fhd:close()
          return true
      end
      
      function M.formatTable(t)
          local arr = {}
          for k, v in pairs(t) do
              local num_k = tonumber(k)
              if type(v) == "table" then
                  if num_k ~= nil then
                      arr[num_k] = M.formatTable(v)
                  else
                      arr[k] = M.formatTable(v)
                  end
              else
                  if num_k ~= nil then
                      arr[num_k] = v
                  else
                      arr[k] = v
                  end
              end
          end
          return arr
      end
      
      ------------------------------------------------------------------
      local test_arr = {
          [2] = 34,
          [7] = "asd",
          str = "asas"
      }
      
      local fn = system.pathForFile("test.txt", system.DocumentsDirectory)
      local is_succeed = M.save_json_to_file(fn, test_arr)
      print("is_succeed", is_succeed)
      local read_json_from_file = M.load_json_from_file(fn)
      
      for k, v in pairs(read_json_from_file) do
          if test_arr[k]~=v then
              print("not equal")
          end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-09
        相关资源
        最近更新 更多