1

Building a game in SpriteKit, I used SKFieldNode.radialGravityField() with negative strength to simulate a positive charge's electric field. To visualize the fields and their interaction, I used SKView.showFields property and it was working fine.

Now, trying to implement a negative charge, I tried switching all fields to SKFieldNode.electricField(). The behavior of the fields and particles (attraction and repulsion) continued to work perfectly, but the visual representation created by showFields stopped working.

Investigating, the visual representation worked only for radialGravityField and did nothing for all other SKFieldNode options. I tried changing the fields' strength but couldn't reach anything. In addition to the fields physics working fine, I looked for the fields magnitude using SKPhysicsWorld.sampleFields(at:) and they were correct.

Example

Based on XCode's default project for iOS game, this GameScene below can reproduce the behavior. Changing from SKFieldNode.radialGravityField() to SKFieldNode.electricField() makes the fields not show up.

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    private var fieldNode : SKFieldNode?
    
    override func didMove(to view: SKView) {
        view.showsFields = true
        
        self.fieldNode = SKFieldNode.radialGravityField() // Change field type here
        
        if let fieldNode = self.fieldNode {
            fieldNode.strength = 2
        }
    }

    func touchDown(atPoint pos : CGPoint) {
        if let n = self.fieldNode?.copy() as! SKFieldNode? {
            n.position = pos
            self.addChild(n)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches { self.touchDown(atPoint: t.location(in: self)) }
    }
}
4
  • 1
    The showFields debug property is broken. See feedbackassistant.apple.com/feedback/9654037
    – Calin
    Apr 28 at 17:17
  • @Calin the link gives me "Feedback not found" Apr 28 at 19:54
  • Looks like feedback links are private to the team. There has been a bug report filed with Apple 7 months ago, as instructed by an Apple engineer. See developer.apple.com/forums/thread/690730. If you can add your comments there, maybe that will make it more visible and Apple will do something about it.
    – Calin
    Apr 28 at 21:23
  • Did it. Wondering if anyone has implemented a field visualizer like this in SpriteKit. Apr 28 at 22:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.