Trigger SpriteKit Gameplay When an Animation Finishes
Give a trap a visible warning, then use a cycle-end callback to decide when it can hit the hero.
This example shows how to build a new trap with Scroller2d’s existing obstacle callbacks. Desert Ruins is a place to try the pattern with its terrain and controls.
Give the obstacle a named callback
On an obstacle definition, add an item to the obstacle’s cycleEndProperties and set its callbackFunction to trapStrike. The creation helper passes that definition to the runtime sprite. Leave obstacleAffected unset to target the obstacle itself, and leave removal off for a reusable trap.
If the cycle-end item specifies an effect atlas, Scroller2d runs that effect before delivering the callback. For an immediate strike after the warning, put the warning in a separate action and leave the cycle-end atlas unset.
Connect the warning to the strike
For a custom SpriteKit warning action, explicitly call executeCycleEnd() when the action completes. That gives you a clear trigger point even when the warning takes a different amount of time on another obstacle.
// trap is a placed SKSpriteNode2dBase; warning is its SKAction.
trap.run(.sequence([
warning,
.run { [weak trap] in trap?.executeCycleEnd() }
]), withKey: "trapWarning")
In the scene’s SceneBaseCustomDelegate, handle the specific callback and use the object to identify the trap that fired.
func manageSceneCallbacks(action: callbackType, function: String, object: Any?) {
guard action == .cycleEnd, function == "trapStrike",
let trap = object as? SKSpriteNode2dBase else { return }
beginStrike(for: trap)
}
beginStrike(for:) is game code. It can open a short damage window, close it during recovery, and ignore duplicate hits in the same strike. Keep that rule in one place while varying each trap’s warning animation or timing.
To build the first version in Desert Ruins, open GameScene.swift. Add the following properties and method to GameScene, then call that method from the trapStrike callback. The example uses the same clock as step(_:).
private var strikeEnds: [ObjectIdentifier: TimeInterval] = [:]
private func beginStrike(for trap: SKSpriteNode2dBase) {
strikeEnds[ObjectIdentifier(trap)] = clock + 0.15
}
In step(_:), check player overlap with each active trap’s hit region before its deadline. Remove the entry when it hits or expires and clear the dictionary when rebuilding the level. For a trap using this rule, route its damage through the window so the generic hazard contact can’t hurt the player during the warning. A frame-based overlap check also catches a player who entered before the strike and is still inside.
Verify the timing in play
Enter the trap’s range during the warning, at the strike, and during recovery. Only the strike should deal damage. Shorten the warning animation and check that the strike moves with it. Trigger two placements in quick succession to confirm that each callback identifies its own trap.
Review how the sample switches animation states
Where Scroller2d saves work
A SpriteKit sequence can call a closure at the end of a warning. A Scroller2d callback name lets several obstacle definitions route completion to the same scene handler. Reuse one strike rule while designers tune the warning on each base obstacle. The game still owns the damage window and recovery behavior.