Search code examples
iossprite-kitskphysicsbodysknodeskfieldnode

Sprite-Kit: How to visualize an SKFieldNode


I am currently working on a magnetic field within my Sprite Kit game. Here's the initialization code:

    private func initMagneticField(_ object: TileMapObjectMagneticField) {
        let field = SKFieldNode.magneticField()
        field.strength = 0.75
        field.position = object.location
        field.region = SKRegion(size: CGSize(width: 320.0, height: 320.0))
        field.categoryBitMask = Globals.Categories.magneticField.rawValue
        stage?.addChild(field)
    }

The magnetic field works so far, but I am unsure how to position the region property of the SKFieldNode object.

Switching on physical visualisation does not show up the region: SKRegion nor the field node itself and the region property has neither size nor position properties:

self.skView.showsPhysics = true

How to you visualise the field itself or at least the region property for debugging purposes ?


Solution

  • Add a child: field.addChild(SKSpriteNode(color:SKColor.red, size:CGSize(width: 320.0, height: 320.0)))

    Alternatively add the following to your view controller

    Swift:

    skView.showsFields = true
    

    ObjC:

    skView.showsFields = YES;