Build Ranged Enemies in SpriteKit with Skypirates
Use the Skypirates demo to see how Scroller2d decides when a raider or patrol boat fires and what happens when a round hits.
Choose Skypirates on the demo downloads page. The ZIP includes an iOS project and an editable LevelEditor workspace. Run Skypirates.xcodeproj to try the encounter, then open Levels/Skypirates.plist from the workspace to change the rules.
Set when raiders and patrol boats fire
In the Skypirates LevelEditor workspace, open the raider or ship in Object Editor. Under Target Distances → Attack, the X field stores a 650-point range. Under Ranged Attack, radial makes that value a radius, so a pilot flying above, below, or diagonally from an enemy can enter range.
| Enemy | Windup | Recovery | Cooldown |
|---|---|---|---|
| Raider | 0.3 s | 0.2 s | 1.8 s |
| Patrol boat | 0.3 s | 0.2 s | 2.5 s |
Each enemy owns a RangedAttackController. In GameScene.swift, the scene updates it each frame. Scroller2d checks range, rechecks the target before release, and handles the timers. The demo callback aims and launches the round. Aircraft steering stays in the demo, and this sample leaves the optional obstruction check off.
enemy.attack.update(at: time, source: enemy.node, target: player,
range: enemy.attackRange, policy: enemy.attackPolicy) { target in
let aim = CGPoint(x: target.position.x - enemy.node.position.x,
y: target.position.y - enemy.node.position.y)
fire(from: enemy.node, hostile: true, impact: enemy.impact, aim: aim)
}
Under Projectiles → Impact, both sides use automatic removal on a valid hit. Pilot rounds count hits against enemies. Enemy rounds trigger the demo’s hull protection rules.
To see what changes through configuration alone, lower the patrol boat’s attack range in LevelEditor, save, run the sample’s sync-level.sh, and rebuild. Approach a boat from the side and then diagonally. It will wait until you fly closer before it starts winding up.
Start with Skypirates’ hull protection
Open Skypirates iOS Demo/Skypirates/GameScene.swift. Search for didResolveProjectileImpact in the nested Hooks class. It forwards the event to projectileImpact(_:), which calls damagePlayer() for hits on the pilot. That method checks invulnerable before reducing hull health. On a damaging hit it sets a 1.2-second protection window. Change that value first and see how quickly consecutive hits can damage the pilot.
func didResolveProjectileImpact(_ impact: ProjectileImpact) {
game?.projectileImpact(impact)
}
For a shield that absorbs rounds while allowing rounds to pass through during temporary invulnerability, open the raider and ship definitions in Object Editor. Under Projectiles → Impact, turn off Consume On Impact and leave Applies Hit off. Save and run sync-level.sh. In projectileImpact(_:), update the existing pilot branch like this and keep the enemy branch for pilot shots.
if impact.target === player {
hostileImpacts += 1
if shieldIsActive {
impact.consume()
} else if invulnerable <= 0 {
impact.consume()
damagePlayer()
}
}
Add private var shieldIsActive = false to GameScene. For a first test, set it to true and fly into enemy fire. The rounds disappear and the hull stays full. Set it back to false to test damage and the protection window. To build a timed shield, start by following how step(_:) counts down invulnerable.
Respawn at a checkpoint after a projectile hit
Start in the downloadable Desert Ruins demo at Desert Ruins iOS Demo/DesertRuins/GameScene.swift. Search for case Body.checkpoint to see how the beacon records a safe position in respawn. Then find pendingRespawn in step(_:). That branch clears the flag, moves the player, and updates the HUD.
To reuse that path for a new projectile, configure its impact to target the hero’s physics category. Enable Applies Hit to use the hero’s Scroller2d hit limit and leave Consume On Impact on. Add didResolveProjectileImpact(_:) to the nested SceneHooks adapter and forward it to this new method on GameScene.
func didResolveProjectileImpact(_ impact: ProjectileImpact) {
scene?.handleProjectileImpact(impact)
}
private func handleProjectileImpact(_ impact: ProjectileImpact) {
guard impact.target === player, impact.reachedHitLimit == true,
!won, !pendingRespawn else { return }
pendingRespawn = true
}
The next update uses the same respawn path as the existing hazards. First try a hit limit of one and confirm that a lethal shot returns the hero to the level start before the beacon is lit and to the beacon afterward. If your game already tracks health separately, leave Applies Hit off and set pendingRespawn when your own health calculation reaches zero.
Follow the full checkpoint and respawn example in Desert Ruins
Extend the attack rules
Set exitDistance above the attack range to keep enemies from switching states repeatedly at the edge of range. Set obstructionMask to the physics categories that block a shot. The controller checks the line to the target again at release, so a target that reaches cover during windup can cancel the pending shot.
Let enemies hold fire during temporary invulnerability
In Skypirates, find enemy.attack.update in GameScene.step(_:). To make enemies hold fire during the pilot’s existing protection window, add the isEligible argument shown below and keep the existing release callback. Scroller2d checks eligibility during windup and again before release. Rounds already in flight still go through the impact rules.
enemy.attack.update(at: time, source: enemy.node, target: player,
range: enemy.attackRange, policy: enemy.attackPolicy,
isEligible: { _ in invulnerable <= 0 }) { target in
let aim = CGPoint(x: target.position.x - enemy.node.position.x,
y: target.position.y - enemy.node.position.y)
fire(from: enemy.node, hostile: true, impact: enemy.impact, aim: aim)
}
For a side-scroller, use horizontal targeting. The enemy base can use targetDistances.near to start pursuit along X, maximumRange to bound that pursuit, and verticalTolerance to limit how far above or below the shooter a target can be.
Use built-in projectile flight in your own game
For a shooter that uses SKSpriteNodeEnemy2dBase, pass a target to CreateCharacterHelper and call the enemy’s update(at:) each frame. The enemy base launches the configured projectiles. Set artwork, size, and optional physics shapes in the character’s projectiles list, then choose a flight mode.
| Flight | What to set | What happens at release |
|---|---|---|
| Ballistic | velocityRanges | Chooses a launch impulse from the horizontal distance band. Gravity bends the flight into an arc. |
| Straight | launchSpeed | Aims toward the hero at release and travels without gravity. |
| Powered | launchSpeed and thrust | Starts toward the hero and accelerates along that launch direction. |
Timed shots launched by the enemy base expire after four seconds by default. Set lifetime for a shorter or longer flight. Add impact to let Scroller2d detect hits on the hero and remove the projectile on impact. Set targetMask to use other collision categories. Enable appliesHit to count one hit against a Scroller2d target’s hit limit.
See how to reuse the same enemy definition across levels
Where Scroller2d saves work
Without the controller, each shooter needs its own range check, windup, release check, and cooldown. Skypirates shares one controller between raiders and patrol boats. A designer changes the radius or timing in LevelEditor while both enemies keep the same update call. The impact helper also shares contact filtering and bullet removal across both teams.