Skip to content

Instantly share code, notes, and snippets.

@chrisdugne
Created April 8, 2019 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdugne/e62845061360c88c9dc7361120e5f1bf to your computer and use it in GitHub Desktop.
Save chrisdugne/e62845061360c88c9dc7361120e5f1bf to your computer and use it in GitHub Desktop.
Demo corona spritesheet animation
local Spirit = {}
local idle = {
name = 'idle/18',
frames = {
{x = 0, y = 0, width = 111,height = 172},
{x = 111, y = 0, width = 111,height = 172},
{x = 222, y = 0, width = 111,height = 172},
{x = 0, y = 172,width = 111,height = 172},
{x = 111, y = 172,width = 111,height = 172},
{x = 222, y = 172,width = 111,height = 172},
{x = 333, y = 0, width = 111,height = 172},
{x = 333, y = 172,width = 111,height = 172}
},
sheetContentWidth = 444,
sheetContentHeight = 344
}
local glasses = {
name = 'with-glasses',
frames = {
{x = 0, y = 0, width = 111, height = 172}
},
sheetContentWidth = 111,
sheetContentHeight = 172
}
local eat = {
name = 'eat/18',
frames = {
{x = 0, y = 0, width = 112, height = 178},
{x = 112, y = 0, width = 112, height = 178},
{x = 224, y = 0, width = 112, height = 178},
{x = 336, y = 0, width = 112, height = 178},
{x = 448, y = 0, width = 112, height = 178},
{x = 560, y = 0, width = 112, height = 178},
{x = 672, y = 0, width = 112, height = 178},
{x = 0, y = 178, width = 112, height = 178},
{x = 112, y = 178, width = 112, height = 178},
{x = 224, y = 178, width = 112, height = 178},
{x = 336, y = 178, width = 112, height = 178},
{x = 448, y = 178, width = 112, height = 178},
{x = 560, y = 178, width = 112, height = 178},
{x = 672, y = 178, width = 112, height = 178},
{x = 0, y = 356, width = 112, height = 178},
{x = 112, y = 356, width = 112, height = 178},
{x = 224, y = 356, width = 112, height = 178},
{x = 336, y = 356, width = 112, height = 178},
{x = 448, y = 356, width = 112, height = 178}
},
sheetContentWidth = 784,
sheetContentHeight = 534
}
local turn = {
name = 'turn/18',
frames = {
{x = 0, y = 0, width = 111, height = 172},
{x = 111, y = 0, width = 111, height = 172},
{x = 222, y = 0, width = 111, height = 172},
{x = 333, y = 0, width = 111, height = 172},
{x = 444, y = 0, width = 111, height = 172},
{x = 555, y = 0, width = 111, height = 172},
{x = 666, y = 0, width = 111, height = 172},
{x = 777, y = 0, width = 111, height = 172},
{x = 0, y = 172, width = 111, height = 172},
{x = 111, y = 172, width = 111, height = 172},
{x = 222, y = 172, width = 111, height = 172},
{x = 333, y = 172, width = 111, height = 172},
{x = 444, y = 172, width = 111, height = 172},
{x = 555, y = 172, width = 111, height = 172},
{x = 666, y = 172, width = 111, height = 172},
{x = 777, y = 172, width = 111, height = 172},
{x = 0, y = 344, width = 111, height = 172},
{x = 111, y = 344, width = 111, height = 172},
{x = 222, y = 344, width = 111, height = 172},
{x = 333, y = 344, width = 111, height = 172},
{x = 444, y = 344, width = 111, height = 172},
{x = 555, y = 344, width = 111, height = 172},
{x = 666, y = 344, width = 111, height = 172},
{x = 777, y = 344, width = 111, height = 172},
{x = 0, y = 516, width = 111, height = 172},
{x = 111, y = 516, width = 111, height = 172},
{x = 222, y = 516, width = 111, height = 172},
{x = 333, y = 516, width = 111, height = 172},
{x = 444, y = 516, width = 111, height = 172},
{x = 555, y = 516, width = 111, height = 172},
{x = 666, y = 516, width = 111, height = 172},
{x = 777, y = 516, width = 111, height = 172},
{x = 0, y = 688, width = 111, height = 172},
{x = 111, y = 688, width = 111, height = 172},
{x = 222, y = 688, width = 111, height = 172},
{x = 333, y = 688, width = 111, height = 172},
{x = 444, y = 688, width = 111, height = 172}
},
sheetContentWidth = 888,
sheetContentHeight = 860
}
local cry = {
name = 'cry/18',
frames = {
{x = 0, y = 0, width = 111, height = 172},
{x = 111, y = 0, width = 111, height = 172},
{x = 222, y = 0, width = 111, height = 172},
{x = 333, y = 0, width = 111, height = 172},
{x = 444, y = 0, width = 111, height = 172},
{x = 555, y = 0, width = 111, height = 172},
{x = 666, y = 0, width = 111, height = 172},
{x = 777, y = 0, width = 111, height = 172},
{x = 888, y = 0, width = 111, height = 172},
{x = 0, y = 172, width= 111, height = 172},
{x = 111, y = 172, width = 111, height = 172},
{x = 222, y = 172, width = 111, height = 172},
{x = 333, y = 172, width = 111, height = 172},
{x = 444, y = 172, width = 111, height = 172},
{x = 555, y = 172, width = 111, height = 172},
{x = 666, y = 172, width = 111, height = 172},
{x = 777, y = 172, width = 111, height = 172},
{x = 888, y = 172, width = 111, height = 172},
{x = 0, y = 344, width= 111, height = 172},
{x = 111, y = 344, width = 111, height = 172},
{x = 222, y = 344, width = 111, height = 172},
{x = 333, y = 344, width = 111, height = 172},
{x = 444, y = 344, width = 111, height = 172},
{x = 555, y = 344, width = 111, height = 172},
{x = 666, y = 344, width = 111, height = 172},
{x = 777, y = 344, width = 111, height = 172},
{x = 888, y = 344, width = 111, height = 172},
{x = 0, y = 516, width= 111, height = 172},
{x = 111, y = 516, width = 111, height = 172},
{x = 222, y = 516, width = 111, height = 172},
{x = 333, y = 516, width = 111, height = 172},
{x = 444, y = 516, width = 111, height = 172},
{x = 555, y = 516, width = 111, height = 172}
},
sheetContentWidth = 999,
sheetContentHeight = 688
}
local idleSheet = graphics.newImageSheet(
'assets/images/game/spirit/' .. idle.name .. '.png',
idle
)
local glassesSheet = graphics.newImageSheet(
'assets/images/game/spirit/' .. glasses.name .. '.png',
glasses
)
local eatSheet = graphics.newImageSheet(
'assets/images/game/spirit/' .. eat.name .. '.png',
eat
)
local turnSheet = graphics.newImageSheet(
'assets/images/game/spirit/' .. turn.name .. '.png',
turn
)
local crySheet = graphics.newImageSheet(
'assets/images/game/spirit/' .. cry.name .. '.png',
cry
)
local sequences = {
{
name = idle.name,
sheet = idleSheet,
start = 1,
count = #idle.frames,
time = 1000,
loopCount = 0,
loopDirection = 'bounce'
},
{
name = glasses.name,
sheet = glassesSheet,
start = 1,
count = #glasses.frames,
time = 1000,
loopCount = 0,
loopDirection = 'bounce'
},
{
name = eat.name,
sheet = eatSheet,
start = 1,
count = #eat.frames,
time = 800,
loopCount = 0,
loopDirection = 'bounce'
},
{
name = turn.name,
sheet = turnSheet,
start = 1,
count = #turn.frames,
time = 400,
loopCount = 1,
loopDirection = 'bounce'
},
{
name = cry.name,
sheet = crySheet,
start = 1,
count = #cry.frames,
time = 1200,
loopCount = 1,
loopDirection = 'forward'
}
}
function Spirit:create(options)
local spirit = display.newSprite(
options.parent,
idleSheet,
sequences
)
spirit.anchorX = 0.5
spirit.anchorY = 1
spirit.xScale = -1
spirit.x = options.x
spirit.y = options.y
spirit.idle = function()
if(not spirit.setSequence) then return end
spirit:setSequence(idle.name)
spirit:play()
end
spirit.eat = function()
if(spirit.nextSequence) then
timer.cancel(spirit.nextSequence)
end
spirit:setSequence(eat.name)
spirit:play()
spirit.nextSequence = timer.performWithDelay(1600, spirit.idle)
end
spirit.turn = function()
if(spirit.nextSequence) then
timer.cancel(spirit.nextSequence)
end
if(not spirit.setSequence) then return end
spirit:setSequence(turn.name)
spirit:play()
spirit.nextSequence = timer.performWithDelay(800, spirit.idle)
end
spirit.cry = function()
if(spirit.nextSequence) then
timer.cancel(spirit.nextSequence)
end
if(not spirit.setSequence) then return end
spirit:setSequence(cry.name)
spirit:play()
spirit.nextSequence = timer.performWithDelay(1200, spirit.idle)
end
spirit.disappear = function()
spirit:turn()
transition.to(spirit, {
xScale = 0,
yScale = 0,
time = 800,
onComplete = function()
display.remove(spirit)
end
})
end
spirit.appear = function()
spirit:turn()
spirit:scale(0, 0)
transition.to(spirit, {
xScale = -2,
yScale = 2,
time = 800
})
end
return spirit
end
return Spirit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment