I’ve been trying to create a class to handle basic transition animations between different scenes/states in my apps, as I found myself re-writing the same bit of code multiple times for each individual animation. My goal is to simply recreate an image of the current screens assets using setContext() and then sprite that image to slide in and out of the different scenes as I normally have.
The code seemed to function just fine outside of the class, but I’m having a hard time getting things working the same from within. I end up with a nil value in the class draw that has me slightly confused. Any help would be greatly appreciated.
My full project is way too big to post, so I’ve created a simple version of my issue…
--# Main
function setup()
SCENE_MENU = 1
SCENE_INFO = 2
cScene = 1
transAni = Anima(WIDTH,HEIGHT,vec2(WIDTH/2,HEIGHT/2),vec2(-WIDTH/2,HEIGHT/2), SCENE_MENU,SCENE_INFO)
transAni.asset = function() testPage() end
transAni.action = function() testAnimateDone() end
parameter.watch("transAni.img")
parameter.watch("transAni.posA")
parameter.watch("transAni.active")
parameter.watch("transAni.strtA.x")
end
function draw()
background(40, 40, 50)
if transAni.active == false then
testPage()
end
transAni:draw()
end
function testPage()
pushStyle()
fill(255, 255, 255, 255)
sprite("Cargo Bot:Opening Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
text("MENU",WIDTH/2,HEIGHT/1.2)
text("tap screen to start transition animation",WIDTH/2,HEIGHT/2)
popStyle()
end
function testAnimateDone()
print("Transition Animation Complete")
cScene = SCENE_INFO
end
function touched(touch)
if touch.state == ENDED then
transAni:createImg()
transAni:imgAnimate()
end
end
--# Anima
Anima = class()
function Anima:init(cellW,cellH,posA,posB,sceneA,sceneB)
self.cellW = cellW -- img width for setContext
self.cellH = cellH -- img height for setContext
self.img = nil
self.posA = posA -- 1st position of image
self.posB = posB -- 2nd position of image
self.sceneA = sceneA
self.sceneB = sceneB
self.asset = asset -- the create function of the page to mock
self.action = nil -- function after animation completes(switch scenes)
self.active = false -- check for animation currently running
end
function Anima:draw()
--if an image exists then start the animation
if self.strtA then
-- sprite(self.img,self.posA.x,self.posA.y) -- just spriting the image works when uncommented
sprite(self.img,self.strtA.x,self.strtA.y,self.strtA.size) --draw animation (nil value?)
end
end
--
-- Create an image for animating
function Anima:createImg()
self.img = image(self.cellW,self.cellH)
setContext( self.img )
self.asset()
setContext()
print("Image Creation Complete")
return self.img
end
--
-- if animation isnt already active, activate animation
function Anima:imgAnimate()
if not self.active then --if animation isnt active
self.active = true --activate animation
print("active =",transAni.active)
self:imgPath() --follow proper path
end
end
--
-- Create a tween path for the image to follow
function Anima:imgPath()
self.strtA = {self.posA.x, self.posA.y, size = self.cellW} --starting pointA
self.strtB = {self.posB.x, self.posB.y, size = self.cellW} --starting pointB
self.twnA = {self.posA.x, self.posA.y, size = self.cellW} --1st point in path
self.twnB = {self.posB.x, self.posB.y, size = self.cellW} --2nd point in path
if cScene == self.sceneB then
print("open/slide in image")
tween.path(.3, self.strtB, {self.twnB,self.twnA},{loop=tween.loop.once},self:imgAnimateDone()) --Slide in
elseif cScene == self.sceneA then
print("slide out image")
tween.path(.5, self.strtA, {self.twnA,self.twnB},{loop=tween.loop.once},self:imgAnimateDone()) --slide out
end
end
--
-- this runs after the image completes the path
function Anima:imgAnimateDone()
self.active = false --animation deactivate
self.action()
end