Multiple animator/animations on one sprite, run logic after animation finishes, what's the best practice?

Hi! I'm new to Lua and game-making. I'm writing a match-three game on Mac with Lua. I wrapped the sprite into a Lua "class" because there will be many sprites on the game board. The three-matched sprites will do a spin animation loop before getting removed. The player can grab a sprite from the tail and move to a different row. So, there are animators for horizontal and vertical movements as well.

I have two problems:

  1. In the sprite's update() function i'm checking every animator/animation's status and calling moveTo() or setImage(), like
function Candy:update()
    if self.dropPopAnimator ~= nil and not self.dropPopAnimator:ended() then
        self:moveTo(self.dropPopAnimator:currentValue(), self.y)
    end
    if self.dropPopAnimator ~= nil and self.dropPopAnimator:ended() then
        self:setZIndex(0) -- reset Z-index
    end
    if self.yAnimator ~= nil and not self.yAnimator:ended() then
        self:moveTo(self.x, self.yAnimator:currentValue())
    end
    if self.animationLoop ~= nil and self.animationLoop:isValid() then
        self:setImage(self.animationLoop:image())
    end
    if self.mergeAnimator ~= nil and not self.mergeAnimator:ended() then
        self:moveTo(self.mergeAnimator:currentValue(), self.y)
    end
    if self.blinker.running and self.blinker.on then
        -- print("blinking")
        self:setScale(0.9)
    else
end

I don't think this is the right way to go :sweat_smile:

  1. Animator/animations don't have callbacks. The sprites needs to be removed only after they are matched and the match animation is finished. I'm currently doing this by using a timer.performAfterDelay(delay, callback). But because I need to recursively search the board for new matches right after the previous match is removed, I ended up creating another timer within the callback. This is also creating problems which I don't fully understand yet.

What is the best practice to deal with multiple animator/animations on the sprite? And how to run logic right after an animator/animation is finished? Also, how do I avoid nested timers and callbacks?

Check out Nic's Sequence library

2 Likes

Thank you so much! This library solves both of my problems!

Also I meant to say I'm new to Lua and game-making :joy:

2 Likes