如何在Corona SDK中使用id来加载精灵和图像文件?

标题听起来很复杂,但我需要的是类似于这样的东西: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("资金不足")
       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

但现在我知道无法连接显示对象,那么有人知道是否有另一种方法来做到这一点吗?

点赞
用户5331361
用户5331361

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

m.units = {
 {
   img = "sprites/girl.png",
   timeBetweenAtk = 2350,
   name = "girl", --看起来似乎girl变量没有声明,所以在你的代码中你得到的是nil
   cost = 75,
   respawnTime = 3750,
   state = active --可能是同样的问题 - 在这里使用字符串或一些预定义的变量
  }
}

因此,在我的代码中,您得到了 m.units[1].name == "girl"

现在您可以在 spawn 函数中使用它:

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 -- 在这里正确地使用字符串标识符
      local unit = display.newSprite(charWalkingData.sheet, charWalkingData.seq)
    end
  end
  return true
end

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

characters[identifier]
characters["girl"] -- 在这个例子中假设identifier = "girl"
characters.girl
2017-06-06 08:07:52