Draw and Reuse Hitboxes in SpriteKit with LevelEditor
Fit a physics shape to the part of an obstacle that matters, then reuse it wherever you place that obstacle.
Imagine a crate with padding around its artwork or a spinning saw with a transparent border. A hitbox that follows the visible solid area can make landings and near misses feel fairer.
Draw the base physics shape
- Open the base obstacle in Object Editor. Enable Use Physics and choose Edit Physics for its physics entry.
- Use a rectangle for flat-sided objects, a circle for round hazards, or a polygon for an irregular silhouette. Inspect the fit against the sprite and leave room around details the hero should be able to brush past.
- Save the base obstacle. Add two placements made from that entry and use Base Properties to inspect the geometry they inherit.
A change to the base definition applies wherever a placement inherits that geometry. Use the placement Inspector’s Physics action when a single instance needs its own shape.
Choose what the body does
Geometry describes where an overlap occurs. Collision masks determine what blocks movement, and contact masks determine which overlaps the game hears about. A solid crate needs to block the hero. A pass-through hazard can report contact and let the game decide what happens next.
At runtime, LevelAssemblyHelper resolves the base definition and placement overrides. CreateObstacleHelper builds bodies from the authored shapes and combines multiple shapes into one obstacle body. Games can still adjust masks or hit behavior after assembly.
Check the result in SpriteKit
Enable skView.showsPhysics = true while testing. Approach the obstacle from both sides and check near misses. For a solid obstacle, try landing on the edge. For a contact-only hazard, check that the hero can pass through and that the game receives the intended contact. Then adjust the base shape once and verify both placements.
See how base objects and placement overrides work together
Where Scroller2d saves work
Build the same shape once on a base obstacle and inspect it on any placement. The native excerpt shows body setup for one authored rectangle. With Scroller2d the factory reads the selected base geometry and masks, and the scene attaches the returned node.
SpriteKit body setup (6 lines)
let body = SKPhysicsBody(rectangleOf: hitboxSize, center: hitboxCenter)
body.isDynamic = false
body.categoryBitMask = categoryMask
body.collisionBitMask = collisionMask
body.contactTestBitMask = contactMask
sprite.physicsBody = body
With a LevelEditor obstacle definition (2 lines)
let obstacle = CreateObstacleHelper(ld: definition, node: world, scene: self)
world.addChild(obstacle)
The excerpts count scene code only. The rectangle and masks must be authored first, in code or in LevelEditor.