I was forum digging and I discovered two ways to use sprite sheets that work but not with every sprite sheet.
https://codea.io/talk/discussion/comment/13909#Comment_13909
I can get the caveman sprite sheet to work both ways but not the Wizard.
Just wondering what I’m doing wrong.
The wizard image is 2000x200
Each frame is 200x200
10 frames in total
For Main
function setup()
local img = readImage(asset.documents.Dropbox["Photo Apr 09, 7 51 25 PM.png"])
wizard = Spritesheet(img,WIDTH,HEIGHT/2,10,1,200,200,0,10)
end
function draw()
background(40, 40, 50)
wizard:draw()
end
function touched(touch)
wizard:touched(touch)
end
Spritesheet class
Spritesheet = class()
function Spritesheet:init(img,x,y,rows,cols,frameWidth,frameHeight,frame,totalFrames)
self.x = x
self.y = y
self.frame = frame
self.totalFrames=totalFrames
self.frameWidth = frameWidth
self.frameHeight = frameHeight
self.frameRows = rows
self.frameCols = cols
self.frameSize = vec2(self.frameWidth,self.frameHeight)
self.counter = 0
self.mesh = mesh()
--self.texture = img
self.mesh.texture = img -- self.texture
self.mesh:addRect(0,0,self.frameWidth,self.frameHeight)
end
function Spritesheet:draw()
self.counter = self.counter + DeltaTime
if self.counter > 1/30 then
self.frame = self.frame + 1
if self.frame > self.totalFrames then
self.frame=0
end
self.x = self.x -1
colUnit = 1/self.frameCols
rowUnit = 1/self.frameRows
row = math.floor(self.frame / self.frameRows)
col = math.floor(self.frame % self.frameCols)
self.mesh:setRectTex(1,
col * colUnit,
( 1-rowUnit) - row * rowUnit,
colUnit,
rowUnit)
self.mesh:setRect(1,self.x,self.y,self.frameWidth,self.frameHeight)
self.counter = 0
end
self.mesh:draw()
end
function Spritesheet:touched(touch)
end