标题听起来令人困惑,但我要找的是这样的: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
但现在我知道连接显示对象是不可能的,所以有人知道是否有另一种方法可以做到这一点。
答案 0 :(得分:0)
我认为你想使用字符串作为标识符。在您的设置中:
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]
如果您的标识符变量包含字符串,即&#34; girl&#34;这个电话是相似的:
characters[identifier]
characters["girl"] -- assuming identifier = "girl" in this example
characters.girl