I have problem with physics body. I have about 20 nodes spawning randomly to view. Every time the node spawns I set the object's physics, like this:
func showObject(){
let texture = SKTexture(imageNamed: "A.png")
object = SKSpriteNode(texture: texture)
object.name = "A"
object.position = CGPoint(x: 0, y: self.frame.width)
object.setScale(0.7)
object.zPosition = 2
object.run(moveAndRemove)
object.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
object.physicsBody?.categoryBitMask = PhysicsCategory.Object
object.physicsBody?.collisionBitMask = PhysicsCategory.Object2
object.physicsBody?.contactTestBitMask = PhysicsCategory.Object2
object.physicsBody?.affectedByGravity = false
object.physicsBody?.isDynamic = true
addChild(object)
}
I think this is not optimal, because physicsBody is set everytime when node is spawned. Because of this I have sometimes little flickering - lower fps when app running. When is physicsBody turned off, everything is ok. So I have a simple question. How to set physics body for all 20 nodes after games started and than just spawn them without creating physicsBody again. I try to set physics body with texture straight to SKView but after this, app crash with nil error.
Thanks for any tip.
There is my spawn:
let SpawnObject = SKAction.run({
() in
let randomFunc = [self.showObject, self.showObject1.......]
let randomResult = Int(arc4random_uniform(UInt32(randomFunc.count)))
randomFunc[randomResult]()
})
let delay1 = SKAction.wait(forDuration: 0.9)
let SpawnDelay1 = SKAction.sequence([SpawnObject,delay1])
let SpawnDelayForever1 = SKAction.repeatForever(SpawnDelay1)
self.run(SpawnDelayForever1)
let distance = CGFloat(self.frame.height + 200)
let moveObject = SKAction.moveBy(x: -distance, y: 0, duration: TimeInterval(0.004 * distance))
let removeObject = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([moveObject,removeObject])