All articles

Create a Moving or Animated Obstacle in SpriteKit

Use the obstacle model and helper to reuse animation and movement setup.

Follow along with the downloadable Desert Ruins iOS demo. Source references in this article point to Desert Ruins iOS Demo/DesertRuins/GameScene.swift. Code excerpts use the sample’s existing types and properties.

Start with a readable hazard

A saw needs a visible animation, a position, and collision behavior that matches the challenge. Adding another hazard often repeats the same setup. Scroller2d’s obstacle definitions gather that configuration so a level can place another instance with its own properties.

Inspect the Desert Ruins saw

Open the obstacle catalog and locate the saw definition referenced by the level. Review its animation frames, timing, repeat behavior, size, and physics settings. LevelAssemblyHelper combines catalog defaults with the placement properties, then calls CreateObstacleHelper internally.

Create the configured node

The demo passes its configureObstacle callback to the assembly helper. The callback receives an already merged model, maps the image name into the asset catalog, and supplies forgiving collision bounds. The API constructs the nodes; the game then applies its solid and hazard rules:

configureObstacleNodes(assembly.obstacles)

The lower-level CreateObstacleHelper remains available when you want to construct individual obstacles directly. Using the assembly helper removes the need to repeat catalog lookup and merging for each placement.

Separate frame timing from movement

Frame timing controls how quickly the artwork changes. Movement settings control where the object travels. Tune one at a time so you can judge each effect. For direct texture playback, manualAnimate supports a frame range, per-frame wait, cycle-end delay, and once or automatic repetition. The sample’s animation article explains how that helper fits into character state selection.

Test a second placement

Duplicate the saw in a recording copy of the level. First keep it stationary and verify the animation. Then configure supported movement properties in the editor and inspect the collision body throughout its travel. A hazard contact in this sample triggers respawn, so test the checkpoint path as well as the visible motion.

Run and edit Desert Ruins

Where Scroller2d saves work

In SpriteKit you assemble the motion and timing actions, then attach them to each obstacle you create. Scroller2d reads the base obstacle’s movement and animation settings when it builds the node. A designer can change the timing once and verify every placement that inherits it. The game still decides what happens when the obstacle hits the hero.