Try this in an iOS/macOS SpriteKit project:
let spriteSize = CGSize(width: 32, height: 32) let sprite1 = SKSpriteNode(color: .red, size: spriteSize) let sprite2 = SKSpriteNode(color: .green, size: spriteSize) let physicsBody1 = SKPhysicsBody(rectangleOf: spriteSize) sprite1.physicsBody = physicsBody1 scene.addChild(sprite1) sprite2.physicsBody = physicsBody1 scene.addChild(sprite2)
It will crash during runtime:
Terminating app due to uncaught exception 'Cant add body, already exists in a world'
I am writing a game engine library/package. Due to its flexible design, there is a chance for the user to end up with a situation where the same
SKPhysicsBody may be added to an
SKScene more than once, causing a runtime crash, unless the user is extra-careful at every step.
I want to eliminate the possibility of that crash, and have the app simply log a warning then continue running, but the obvious solutions are not very elegant; I will have to subclass many SpriteKit classes and instruct the users of my library to only inherit from those subclasses, which defeats certain goals of the engine.
do-try-catchdoes not help here because
No calls to throwing functions occur within 'try' expression
I cannot do anything with
SKNode.physicsBodyvia an extension, because you cannot override properties or add observers in extensions.