Game Development Reference
In-Depth Information
ting all of your sprite's textures. The first step to make this happen is to create an SKTex-
tureAtlas and pass it to nodes so they can retrieve their own textures. Add this line of
code immediately before the first init() method in the GameScene class:
let textureAtlas = SKTextureAtlas(named: "sprites.atlas")
After you have created the texture atlas, you need to change the init() method of each
of the recently created SKSpriteNode s to take an SKTextureAtlas as a parameter.
Once the SKSpriteNode has a reference to the SKTextureAtlas , then you can
change the texture-loading code in each node to use this passed-in textureAtlas . The
following init() method shows this change made to the SpaceMan class:
init(textureAtlas: SKTextureAtlas) {
let texture = textureAtlas.textureNamed("Player")
super.init(texture: texture, color:
UIColor.clearColor(), size: texture.size())
physicsBody = SKPhysicsBody(circleOfRadius: size.width
/ 2)
physicsBody!.dynamic = false
physicsBody!.linearDamping = 1.0
physicsBody!.allowsRotation = false
physicsBody!.categoryBitMask = CollisionCategoryPlayer
physicsBody!.contactTestBitMask =
CollisionCategoryPowerUpOrbs
| CollisionCategoryBlackHoles
physicsBody!.collisionBitMask = 0
}
Notice the init() method's parameter list now takes an SKTextureAtlas parameter,
and the first line of the init() method uses this SKTextureAtlas to load the Play-
er texture. Also note that the init() method no longer overrides the default init() ,
and therefore I have removed the override keyword. Make this change to the space-
man's init() method and let's go back to GameScene 's init() method. To use the
new SpaceMan init() method, you need to change the construction of the
SpaceMan to look like the following line:
self.playerNode = SpaceMan(textureAtlas: self.textureAtlas)
Search WWH ::




Custom Search