如何在 Corona SDK 中使用 id 加载精灵表和 img 文件?

How to use an id to load sprite sheets and img files in Corona SDK?

标题听起来很混乱,但我要找的是这样的:https://www.youtube.com/watch?v=GhI98-HJk6o

只看第一分钟,注意当玩家点击底部方块时会生成相应的单位。我正在尝试在 Corona SDK 中实现这一点,但最近我遇到了很多问题,希望大家能提供帮助。

我尝试做的是:

local m = {} -- characters.lua

m.units = {
   {img = "sprites/girl.png", timeBetweenAtk = 2350, name = girl, cost = 75, respawnTime = 3750, state = active}
}

m.girl = {}
m.girl.walking = {}
local data = {width = 309, height = 494, numFrames = 10, sheetContentWidth = 1545, sheetContentHeight = 988}
m.girl.walking.sheet = graphics.newImageSheet("sprites/walking-girl.png", data )
m.girl.walking.seq = {name = "walking", start = 1, count = 10, loopCount = 0, loopDirection = "forward"}

m.girl.primaryAtk = {}
local data = {width = 385, height = 477, numFrames = 10, sheetContentWidth = 1925, sheetContentHeight = 954}
m.girl.primaryAtk.sheet = graphics.newImageSheet("sprites/melee-girl.png", data)
m.girl.primaryAtk.seq = {name = "melee", start = 1, count = 10, loopCount = 0, loopDirection = "forward"}

m.girl.idle = {}
local data = {width = 271, height = 473, numFrames = 10, sheetContentWidth = 813, sheetContentHeight = 1892}
m.girl.idle.sheet = graphics.newImageSheet("sprites/idle-girl.png", data)
m.girl.idle.seq = {name = "idle", start = 1, count = 10, loopCount = 0, loopDirection = "forward"}

return m

然后我在 main.lua 中创建了一个函数来调用正确的精灵:

local characters = require "characters"

for k = 1, #imgSet do
  local function spawnCharacter(event)
     local identifier = characters.units[k].name
     if event.phase == "began" then
       if moneyCount < characters.units[k].cost then
          print("insufficient funds")
       else
          moneyCount = moneyCount - characters.units[k].cost
          moneyText.text = moneyCount .. " / " .. moneyWallet
          local unit = display.newSprite(characters.identifier.walking.sheet, characters.identifier.walking.seq)
       end
     end
     return true
  end
  imgSet[k]:addEventListener("touch", spawnCharacter)
end

但现在我知道无法连接显示 objects,所以有人知道是否有其他方法可以做到这一点。

我想你想使用字符串作为标识符。在您的设置中:

m.units = {
 {
   img = "sprites/girl.png", 
   timeBetweenAtk = 2350, 
   name = "girl", --seems like girl variable not declared so in your code you got nil
   cost = 75, 
   respawnTime = 3750, 
   state = active -- probably same issue - use string here or some pre-defined variable
  }
}

所以用我的代码你得到了m.units[1].name == "girl"

现在你可以在生成函数中使用它了:

local function spawnCharacter(event)
  local charToSpawn = characters.units[k]
  local identifier = charToSpawn.name
  if event.phase == "began" then
    if moneyCount < charToSpawn.cost then
      print("insufficient funds")
    else
      moneyCount = moneyCount - charToSpawn.cost
      moneyText.text = moneyCount .. " / " .. moneyWallet

      local charWalkingData = characters[identifier].walking -- proper use of string identifier here
      local unit = display.newSprite(charWalkingData.sheet, charWalkingData.seq)
    end
  end
  return true
end

注意这一行:characters[identifier] 如果您的标识符变量包含字符串,即 "girl" 此调用是类似的:

characters[identifier]
characters["girl"] -- assuming identifier = "girl" in this example
characters.girl