Problem with conventional approach: you want to hide a node by fading it out. But when you do that sometimes its already fading in, or partly faded out. You need to cancel any previous animation and then start the new animation.
Solution: use a state machine to capture the potential transitions between visible, fading in/out, and hidden.
To add to a node
var visibilityState: FaderStateMachine?
init() {
super.init()
visibilityState = FaderStateMachine(self, 0.4)
}
Then to hide:
visibilityState.enterState(Hiding)
or
visibilityState.enterState(Showing)
No adjustment is made in the case where you do a "u-turn" - that is going from Showing straight to Hiding or vice-versa. In that case if the "Showing" animation is e.g. 1/3 done and alpha is 0.33, when you start hiding, the duration of the animation should really be reduced to 1/3 of its normal duration.
Because no adjustment is made, if duration is large players might notice a discrepancy in animation speed in the "u-turn" case. This would not be hard to but I have not bothered as my durations are small.