For our project i wrote a function that generates a sprite sheet instance by checking the set imageSuffix inside the config.lua.
Not the most sophisticated way yet (since i did not spend too much time on it), but maybe it will help you guys.
It is (so far) not a very dynamic code. So sadly you need to fullfill some requirements:
- the spritesheet need to have a 1px spacing between the frame pictures (but you can edit the function to conform your project)
- the spritesheet must be in the png format for every set resolution (config.lua) since i do not check wether the file exists (should be easy to implement too, actually ;-))
first the string split file (save as str.lua)
module(..., package.seeall)
split = function(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last\_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last\_end = e+1
s, e, cap = str:find(fpat, last\_end)
end
if last\_end \<= #str then
cap = str:sub(last\_end)
table.insert(t,cap)
end
return t
end
and here is the function:
require("config")
require "sprite"
str = require("str")
function create\_spritesheet(\_file, \_w, \_h, \_frames, \_duration)
-- split to append suffix
local file = str.split(\_file,".png")
local scalex = display.contentScaleX
local scaley = display.contentScaleY
local upscalex = 1/scalex
local upscaley = 1/scaley
-- searching for the right scale factor and suffix
local chosen\_scale = 1
local chosen\_suffix = ""
for k,v in pairs(application.content.imageSuffix) do
if math.abs(upscalex-v) \< math.abs(upscalex-chosen\_scale) then
chosen\_scale = v
chosen\_suffix = k
end
end
local chosen\_w = ((\_w-1)\*chosen\_scale)+1
local chosen\_h = ((\_h-1)\*chosen\_scale)+1
local new\_file = file[1]..chosen\_suffix..".png"
local sheet = sprite.newSpriteSheet( new\_file, chosen\_w, chosen\_h )
local spriteSet = sprite.newSpriteSet(sheet, 1, \_frames)
sprite.add( spriteSet, "animation", 1, \_frames, \_duration, 0 )
local instance = sprite.newSprite( spriteSet )
-- down or upscale the sprite
instance:scale(1/chosen\_scale,1/chosen\_scale)
instance:prepare("animation")
return instance
end
example how to call the function
local my\_sprite = create\_spritesheet("images/spaceship.png", 40, 40, 15, 1000)
my\_sprite:play()
hf
Robin
[import]uid: 102950 topic_id: 19979 reply_id: 78720[/import]