加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SpriteA4.lua 2.62 KB
一键复制 编辑 原始数据 按行查看 历史
FreeBlues 提交于 2015-02-13 00:37 . Update SpriteA4.lua
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!')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化