Game Development Reference
In-Depth Information
Using SKActions to Animate Sprites
In this section I will show you how to leverage SKAction s to animate the black hole
nodes that you just added to the scene. In doing so, I will be introducing you to a couple
of new classes: SKTexture and SKTextureAtlas .
An SKTexture is an object that holds an image that is used by SKSpriteNode s,
SKShapeNode s, or as the particles created by an SKEmitterNode . You have been us-
ing SKTexture throughout this topic when you have created an SKSpriteNode . Each
image used to create an SKSpriteNode was internally represented as an SKTexture .
An SKTextureAtlas is a collection of SKTexture objects created from a texture at-
las stored in an application's resource bundle. Go back to your project in Xcode and open
the folder named sprites.atlas . You will see all of the images, except background
images, used in this game. If you wanted to load all of the images in this atlas folder in-
to an SKTextureAtlas , you would execute the following code:
let textureAtlas = SKTextureAtlas(named: "sprites.atlas")
This single line reads all of the individual files in the sprites.atlas folder and adds
them to the SKTextureAtlas each as an SKTexture that can be looked up by the
original file name. Add this line to the top of the addBlackHolesToForeground()
method, and let's move on.
To retrieve an SKTexture , you would use the SKTextureAt-
las.textureNamed() method, passing it the name of the texture you want to re-
trieve. An example of this that retrieves the first black hole texture in the textureAt-
las is shown here:
let frame0 = textureAtlas.textureNamed("BlackHole0")
This line of code retrieves the SKTexture represented by the name BlackHole0 and
stores it in the constant frame0 .
To create an animation using all of the black hole images, you would retrieve each of
them from the textureAtlas and add them to an array that represents the order each
texture will be displayed in the animation. This code is shown in the following snippet:
let frame0 = textureAtlas.textureNamed("BlackHole0")
let frame1 = textureAtlas.textureNamed("BlackHole1")
Search WWH ::




Custom Search