Here’s some code that will create a sprite sheet and read the sprites from the sheet. I have no use for or used a sprite sheet, so this code doesn’t go into much detail for creating or reading sprite sheets. This is set up to read the sprites from “Planet Cute” and create a sprite sheet in “Documents” called “xxx”. Almost all of the sprites in “Planet Cute” have a size of 101x171, so I used that pack as an example. Only sprites with a size of 101x171 are put in the sheet. If you want to use this to create other sprite sheets, you’ll have to modify some of the variable to fit your needs. Some things that need to change are the sprite sizes you want to save, the size of the sprite sheet ( multiples of the sprites plus a little more ), and the number of sprites in the sheet when you read them back. If you want to use this and have any questions, let me know. When this program starts, tap on “pack” to create a sprite sheet. Once created, you can move the sprite sheet around to view all of the sprites in it, or flip the “create” slider at the top of the parameters to “off”, then each time you tap the screen it will display another sprite from the sheet. Since I don’t use sprite sheets, I didn’t put much effort into writing this code, so there may be easier ways of doing this. I just wanted to see what was involved in doing this.
function setup()
parameter.boolean("create",true,choose)
end
function choose()
if create then
setupCreateSheet()
else
setupReadSheet()
end
end
function setupCreateSheet()
output.clear()
spriteMode(CORNER)
print("Create sprite sheet.")
print("\
Tap pack to start.")
print("\
You can move the sprite sheet")
print("to view all of the sprites on it.")
parameter.text("spritePack","Planet Cute")
parameter.text("saveName","Documents:xxx")
parameter.action("pack",function() pack=true end)
sl=spriteList(spritePack)
spw=101 -- sprite size to pack in sprite list
sph=171
xx=0
yy=0
count=0
tx=20 -- display offset for sprite sheet
ty=20
rd=false
w=925 -- sprite sheet size, > multiples of width, height
h=1200
img=image(w,h) -- sprite sheet image
pack=false
end
function setupReadSheet()
output.clear()
sp=readImage("Documents:xxx") -- get the sprite sheet
if sp then
spw=sp.width
sph=sp.height
end
count=0
tx=0
ty=0
xx=1
yy=1
-- sprite width and height
sw=101
sh=171
img1=image(sw,sh) -- image area for sprite
print("Read sprite sheet.")
print("\
Tap the screen to read each")
print("sprite from the sprite list.")
end
function draw()
if create then
background(37, 125, 102, 73)
noFill()
stroke(255, 0, 0, 255)
strokeWidth(10)
rect(tx-16,ty-16,w+16,h+16)
sprite(img,tx,ty)
readSprites()
else
background(208, 208, 208, 52)
sprite(img1,WIDTH/2,HEIGHT/2)
end
end
function readSprites() -- read each sprite and pack it
if pack then
count=count+1
if count>#sl then -- all sprites read
readComplete()
return
end
sp=readImage(spritePack..":"..sl[count])
-- only pack sprites with this width and height
if sp.width==spw and sp.height==sph then
copySprite()
xx=xx+spw
if xx+spw>w then
xx=0
yy=yy+sph
if yy+sph>h then
packComplete()
end
end
end
end
end
function readComplete() -- sprite sheet complete
pack=false
saveImage(saveName,img)
print("\
Sprite sheet complete")
print(saveName," saved")
end
function copySprite() -- copy sprite to sprite sheet
local r,g,b,a,x,y
for x=1,sp.width do
for y=1,sp.height do
r,g,b,a=sp:get(x,y)
img:set(x+xx,y+yy,r,g,b,a)
end
end
end
function touched(t)
if create then -- creating sprite sheet
-- move sprite sheet around
if t.state==MOVING then
tx=tx+t.deltaX
ty=ty+t.deltaY
end
elseif t.state==BEGAN then
-- check for sprite sheet
if sp==nil then
output.clear()
print("No sprite sheet found.")
return
end
count=count+1
-- check for end of sprite sheet
if count>55 then
print("\
End of sprite sheet reached, starting over.")
count=1
xx=1
yy=1
end
-- get next sprite
img1=sp:copy(xx,yy,sw,sh)
xx=xx+sw
if xx+sw>spw then
xx=1
yy=yy+sh
end
end
end