Here’s the code to put sprites in a sprite sheet and get them back out. For now, the sprites to pack are put in the table. When the program is run, all the sprites are at the lower left of the screen. Just touch a sprite and drag it wherever you want. When you have all the sprites arranged how you want, use the sliders to enclose the sprites in the red lines. That will be the size of the sprite sheet. Start with the top parameter and work your way down. Leave a little space around the sprites. You can change the save image name if you want. When ready, tap the pack parameter. The x,y corner position, width, and height of each sprite is embedded in the lower left of the sprite sheet.
-- pack sprites in a sprite sheet
viewer.mode=STANDARD
function setup()
parameter.integer("xCorner",1,WIDTH//1,0)
parameter.integer("yCorner",1,HEIGHT//1,0)
parameter.integer("xWidth",20,WIDTH//1*2,WIDTH//1*2)
parameter.integer("yHeight",20,HEIGHT//1*2,HEIGHT//1*2)
parameter.text("imageName","temp")
parameter.action("pack",packSprites)
rectMode(CORNER)
spriteMode(CORNER)
dx,dy,sel=0,0,0
-- table of sprites to put in sprite sheet
tabPath={
asset.builtin.Tyrian_Remastered.Blimp_Boss,
asset.builtin.SpaceCute.Star,
asset.builtin.SpaceCute.Beetle_Ship,
asset.builtin.Space_Art.Red_Explosion,
asset.builtin.Cargo_Bot.Codea_Logo,
asset.builtin.Small_World.Beam,
asset.builtin.Space_Art.Red_Ship,
asset.builtin.Planet_Cute.Character_Horn_Girl,
}
tab={}
for z=1,#tabPath do
img=readImage(tabPath[z])
table.insert(tab,{w=img.width,h=img.height,x=0,y=0,path=tabPath[z]})
end
end
function draw()
background(255, 165, 0)
stroke(255)
strokeWidth(2)
noFill()
for a,b in pairs(tab) do
sprite(b.path,b.x+dx,b.y+dy)
rect(b.x-2+dx,b.y-2+dy,b.w+4,b.h+4)
end
stroke(255,0,0)
rect(xCorner+dx,yCorner+dy,xWidth,yHeight)
end
function touched(t)
if t.state==BEGAN then
for a,b in pairs(tab) do
if t.x>b.x+dx and t.x<b.x+b.w+dx and t.y>b.y+dy and t.y<b.y+b.h+dy then
sel=a
break
end
end
end
if t.state==CHANGED then
if sel>0 then
tab[sel].x=(tab[sel].x+t.deltaX)//1
tab[sel].y=(tab[sel].y+t.deltaY)//1
else
dx=dx+t.deltaX
dy=dy+t.deltaY
end
end
if t.state==ENDED then
sel=0
end
end
function packSprites()
tab1={}
img=image(xWidth,yHeight)
setContext(img)
for a,b in pairs(tab) do
temp=readImage(b.path)
table.insert(tab1,b.x-xCorner)
table.insert(tab1,b.y-yCorner)
table.insert(tab1,b.w)
table.insert(tab1,b.h)
for x=1,b.w do
for y=1,b.h do
r,g,bl,a=temp:get(x,y)
img:set(b.x-xCorner+x,b.y-yCorner+y,r,g,bl,a)
end
end
end
packInfo()
setContext()
saveImage(asset.documents.Dropbox..imageName,img)
print("Image saved")
print("Number of sprites "..#tab)
end
function packInfo()
xLoc,yLoc=1,1
r=#tab -- number of sprites
g,b,a=255,255,255
img:set(xLoc,yLoc,r,g,b,a)
xLoc=xLoc+1
for z=1,#tab1 do -- put sprite info into sprite sheet
r=tab1[z]//100
g=tab1[z]%100
img:set(xLoc,yLoc,r,g,b,a)
xLoc=xLoc+1
end
end
The code to extract the sprites.
-- extract sprite from sprite sheet
viewer.mode=FULLSCREEN
function setup()
img=readImage(asset.documents.Dropbox.temp)
tab={}
yy,pos,c=1,1,1
nbr,g,b,a=img:get(1,1) -- number of sprites
for z=1,nbr*4,4 do -- sprites are put in table tab
r,g,b,a=img:get(z+1,yy)
x=r*100+g
r,g,b,a=img:get(z+2,yy)
y=r*100+g
r,g,b,a=img:get(z+3,yy)
w=r*100+g
r,g,b,a=img:get(z+4,yy)
h=r*100+g
tab[pos]=img:copy(x,y,w,h)
pos=pos+1
end
fill(255)
end
function draw()
background(255, 150, 0)
sprite(img,WIDTH/2,HEIGHT-300)
sprite(tab[c],WIDTH/2,150)
text("tap screen to cycle thru sprites",WIDTH/2,HEIGHT-50)
end
function touched(t)
if t.state==BEGAN then
c=c+1
if c>6 then
c=1
end
end
end