Search code examples
swiftsprite-kitcollision-detectionparticle-system

Detect collision between SKNode and Particles?


I have this function that creates some lava:

func setupLava() {
    let emitter = SKEmitterNode(fileNamed: "Lava.sks")!
    emitter.particlePositionRange = CGVector(dx: 200, dy: 0.0)
    emitter.advanceSimulationTime(3.0)
    emitter.zPosition = 4
    emitter.position = CGPoint(x: self.frame.width / 2, y: 300)
    lava.addChild(emitter)

}

And I want to detect when the player collides with it. How do I do that?


Solution

  • From the documentation:

    Particles are not represented by objects in SpriteKit. This means you cannot perform node-related tasks on particles, nor can you associate physics bodies with particles to make them interact with other content. Although there is no visible class representing particles added by the emitter node, you can think of a particle as having properties like any other object.

    So you cannot use SpriteKit to detect collisions with the emitted Lava, but you can associate a physics body to the Lava object and collide with it not it individual emitted nodes. Use categoryContactMask, contactBitMask fields of the physicsBody of your nodes to detect contacts.