【问题标题】:How to use an id to load sprite sheets and img files in Corona SDK?如何使用 id 在 Corona SDK 中加载精灵表和 img 文件?
【发布时间】:2017-11-06 17:20:16
【问题描述】:

标题听起来令人困惑,但我正在寻找的是这样的: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

但现在我知道连接显示对象是不可能的,所以有谁知道是否有其他方法可以做到这一点。

【问题讨论】:

    标签: lua sprite coronasdk lua-table


    【解决方案1】:

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

    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"

    现在你可以在 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 -- proper use of string identifier here
          local unit = display.newSprite(charWalkingData.sheet, charWalkingData.seq)
        end
      end
      return true
    end
    

    关注这一行:characters[identifier] 如果您的标识符变量包含字符串,即“女孩”,则此调用是类似的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      相关资源
      最近更新 更多