Reputation: 43
I am running the following code in the background thread when the app first loads so that the SKPhysicsBody array is all ready when the node is loaded. This is then used in a custom SKAction as per How can I animate a SKPhysicsBody texture from an SKTextureAtlas @mfessenden solution. This dramatically improves performance! This is not so much a problem on newer devices as will mainly be completed during the app intro. But for older devices this may overrun and affect app performance. Is there was way to store the calculated array in the main bundle data or something so the function loop does not need to be run every time. I have tried to store in userDefaults but get an error ( 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object ). What is the best way to store this data so it only needs to be run once?
var Sat1shapes: [SKPhysicsBody] = []
var Sat1Frames: [SKTexture] = []
for frame in Sat1Frames {
let node6 = SKSpriteNode()
node6.physicsBody = SKPhysicsBody(texture: frame, size: CGSize(width: 414 * wR, height: 233 * hR))
Sat1shapes.append(node6.physicsBody!)
}
let Sat2AnimatedAtlas = SKTextureAtlas(named: "satsmall2")
let numImagesSat2 = Sat2AnimatedAtlas.textureNames.count
for i in 0...numImagesSat2 {
let Sat2TextureName = "\(i)"
Sat2Frames.append(Sat2AnimatedAtlas.textureNamed(Sat2TextureName))
}
Upvotes: 1
Views: 37