Build Reusable Enemies and Obstacles Across Levels
Share object definitions while keeping each placement configurable.
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.
When copied setup starts to drift
If each crate or enemy has its own setup code, a change to the hitbox or artwork can leave older placements behind. Scroller2d’s catalogs let you identify a common element by elementName and use that definition for multiple placements.
Define the reusable object
Inspect Assets/ObstacleList.plist and Assets/CharacterList.plist in the sample. Each catalog holds definitions that the level refers to. Choose a stable elementName for the reusable definition and a distinct name for a placed instance when gameplay needs to identify it.
Resolve defaults and overrides
LevelAssemblyHelper.Resources resolves each placement by elementName. Non-nil instance properties override catalog values, nested properties merge, and device records merge by their exact deviceName string. An explicit empty array clears a catalog array. Missing definitions throw. The same resolution is available independently of scene construction:
let obstacle = try data.resources.obstacle(for: entry)
let character = try data.resources.character(for: characterEntry)
These lines illustrate the two separate entry types. During assembly, the demo uses configureObstacle to map artwork names and supply forgiving physics bounds after merging. Its configureObstacleNodes function assigns solid and hazard collision rules to the returned nodes.
Use the character helper
The assembly helper uses CreateCharacterHelper by default. Desert Ruins passes a createCharacter callback that skips its separately controlled player and builds the guardian from the already resolved character model. The guardian factory retains the SDK's enemy animation and behavior, maps the atlas name, and supplies the game's hitbox. Player input, damage rules, and enemy defeat remain in the game.
Try changing a shared definition
Place two instances of the same obstacle in a test level. Change the catalog’s image or size and rebuild both. Then give one instance an override and verify that it differs as intended. Copy the updated catalog together with the level when testing, because a new level file can still reference old bundled definitions.
Where Scroller2d saves work
The base catalog is the place to fix a property shared by several levels. Scroller2d merges placement overrides when loading a level, so a new default reaches placements that inherit it while deliberate exceptions keep their values. Test both an inherited placement and an overridden one after a catalog change.