I'm building my first game using Swift and SpriteKit and want to add a background. The game takes place in space so I wanted to have stars in the background moving at varying speeds. Currently, I'm going for a 3D look by making the larger stars move across screen faster than the smaller ones. Is there an efficient way to do this rather than making a SKNode subclass like this and adding it as a child at the start of DidMoveToView? It seems like this method is pretty intensive but I thought I'd try it before recycling the same image over and over.
class BackGroundAnimation:SKNode{
let theView:SKView
init(aView:SKView){
theView = aView
super.init()
animate()
}
func animate(){
for _ in 1...200{
let randomSize = random(1, max: 3)
var randomPosx = random(1,max: 1000)
randomPosx = randomPosx/1000.0
var randomPosy = random(1,max: 1000)
randomPosy = randomPosy/1000.0
let star:SKSpriteNode = SKSpriteNode(texture:starTexture)
star.setScale(randomSize/60.0)
star.position = CGPoint(x:(theView.scene?.size.width)! * randomPosx,y:(theView.scene?.size.width)! * randomPosy)// (self.scene.size.width)*randomPosx, y:(self.scene.size.height) * randomPosy)
//star.position = CGPoint(x: 200,y: 200)
star.physicsBody = SKPhysicsBody(circleOfRadius: star.size.width/2 )
star.physicsBody?.collisionBitMask = 0
star.physicsBody?.categoryBitMask = 0
star.physicsBody?.contactTestBitMask = 0
star.physicsBody?.linearDamping = 0
star.physicsBody?.velocity = CGVector(dx:1 * randomSize, dy:0)
star.name = "star"
//addChild(star)
self.addChild(star)
self.moveToParent(self.scene!)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Any help would be great.
