代码拉取完成,页面将自动刷新
local bufs = {{name = [===[Main]===],code=[===[
--# Main
-- From:http://codea.io/talk/discussion/4803/spritesheets-animation-in-codea
function setup()
local spritesheet = readImage("Dropbox:runningcat_512x256")
cat = {
idle = animation(spritesheet, 512, 256, {vec2(2,2)}), -- freeze on frame[i]
run = animation(spritesheet, 512, 256),
}
end
function draw()
noSmooth()
spriteMode(CORNER)
background(28, 245, 156, 255)
sprite("Dropbox:retrojungle", 0, 0, WIDTH, HEIGHT)
translate(WIDTH/4, 100)
scale(.75)
cat.run:play()
end
--# animation
animation = class()
function animation:init(spritesheet, width, height, ...)
self.img = type(spritesheet) == "string" and readImage(spritesheet) or spritesheet
self.width = width
self.height = height
self.cols = math.ceil(self.img.width / self.width)
self.rows = math.ceil(self.img.height / self.height)
self.fps = type(arg[1]) == "number" and arg[1] or 24
self.frames = type(arg[1]) == "table" and arg[1] or (type(arg[2]) == "table" and arg[2] or {})
if #self.frames == 0 then
for r = 1, self.rows do
for c = 1, self.cols do
table.insert(self.frames, vec2(c, r))
end
end
end
self.mode = { -- spriteMode(...)
[0] = { -- CORNER
pos = vec2(self.width/2, self.height/2),
size = vec2(self.width, self.height)
},
[1] = { -- CORNERS
pos = vec2(self.width/2, self.height/2),
size = vec2(self.width, self.height)
},
[2] = { -- CENTER
pos = vec2(0, 0),
size = vec2(self.width, self.height)
},
[3] = { -- RADIUS
pos = vec2(0, 0),
size = vec2(self.width*2, self.height*2)
}
}
self.atlas = mesh()
self.mask = self.atlas:addRect(0, 0, 0, 0)
self.atlas.texture = self.img
end
function animation:play()
self.timer = self.timer or ElapsedTime
local n = spriteMode()
i = i or 1
if (ElapsedTime-self.timer) > (1/self.fps) then
self.timer = nil
i = self.frames[i+1] and (i+1) or 1
end
self.atlas:setRect(self.mask, self.mode[n].pos.x, self.mode[n].pos.y, self.mode[n].size.x, self.mode[n].size.y)
self.atlas:setRectTex(self.mask, 1/self.cols*(self.frames[i].x-1), (1/self.rows*(self.rows-self.frames[i].y)), 1/self.cols, 1/self.rows)
self.atlas:draw()
end
]===]}}
for i=1, #bufs do
saveProjectTab(bufs[i].name,bufs[i].code)
end
print('SpriteA4 Installed!')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。