First, create a texture atlas and add it to your project
- create a folder with a .atlas extension in your project directory
- add the helicopter parts images to the folder
- add the folder to your project (by right clicking in the Xcode's File Navigator and selecting "Add Files to "[Your Project]..." option)
Load the texture atlas by adding the following to didMove(to view:SKView)
:
let atlas = SKTextureAtlas(named: "helicopter")
and then create a texture for each part
let body = atlas.textureNamed("body.png")
let rotor = atlas.textureNamed("rotor.png")
let missiles = atlas.textureNamed("missiles.png")
Lastly, assemble the helicopters with the parts. The zPosition
of each part determines the order in which they are drawn.
let startX:CGFloat = -200
for i in 0...10 {
let helicopterBody = SKSpriteNode(texture: body)
helicopterBody.zPosition = 100
helicopterBody.position = CGPoint(x: startX + CGFloat(i*40), y: 0)
let helicopterRotor = SKSpriteNode(texture: rotor)
helicopterRotor.position = CGPoint(x: startX + CGFloat(i*40), y: 0)
helicopterRotor.zPosition = 200
let helicopterMissiles = SKSpriteNode(texture: missiles)
helicopterMissiles.position = CGPoint(x: startX + CGFloat(i*40), y: 0)
helicopterMissiles.zPosition = 0
addChild(helicopterBody)
addChild(helicopterRotor)
addChild(helicopterMissiles)
}
Show the draw count by adding the following to GameViewController:
view.showsDrawCount = true