I wont use a "custom class" on interface builder for an example "Walls".
Drag a SpriteNode and set the custom class : Wall.
On code Wall.swift :
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
self.color = .white // This works
let body = SKPhysicsBody(rectangleOf: frame.size)
body.isDynamic = true
body.allowsRotation = false
body.pinned = false
body.affectedByGravity = true
body.friction = 0.0
body.restitution = 0.0
body.linearDamping = 1.0
body.angularDamping = 1.0
body.mass = 1.0
body.velocity = CGVector(dx: 0, dy: 0)
body.density = 1.0
body.charge = 1.0
body.categoryBitMask = 4294967295
body.collisionBitMask = 4294967295
body.fieldBitMask = 4294967295
body.contactTestBitMask = 0
self.physicsBody = body
}
The sprite change color to white, but physicsBody doesn't work.
What im doing wrong?
When you read the SKScene that contains your custom SKSpriteNode, the SKPhysicBody is also read, and overwrites what you have put in your initialization function. You must therefore put your SKPhysicBody after the SKScene has been read, for example in the awakeFromNib function.