Finally, if this code is based on our other talk here: https://forums.coronalabs.com/topic/69092-how-can-i-require-this
Then the code I’m seeing makes no sense. You can’t concatenate tables or display objects with …
walking.sheet is a sprite sheet instance.
Blex
22
Oh.
Also, this is my current code:
--characters.lua local m = {} m.units = { {img = "sprites/girl.png", timeBetweenAtk = 2350, name = "girl", cost = 75} } 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 for k = 1, #imgSet do local identifier = characters.units[k].name local function spawnCharacter(event) 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
I was just trying to find a good way to spawn an individual unit if there are 10 to choose from.
Blex
23
I have tried concatenating the string into the image file, but I can’t use the data, because I can’t concatenate.
for i = 1, #m.units do identifier = m.units[i].name end function m.spawnUnit(identifier) local unit = ???? end
If this was for only two characters it would be easy, but I want this to work for dozens if not hundreds of characters.
I have one thing to say (based on all your posts)… walk before you run!
Start simple and when you completely understand then add more functionality bit by bit.
Blex
25
You are right, I think I have put too much on top of the building, instead of focusing on the base.
Blex
26
Would you just happen to know where I can find the info for a way to do this?
“concatenation” is probably not the word. seems the real topic s/b this access method:
characters[identifier].walking.sheet
instead of existing:
characters.identifier.walking.sheet
Blex
28
I did not think of that! That could actually work. I’m just having trouble adjusting my code.
Blex
29
Finally got it!
--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"} --main.lua 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