Title says it all. How do i use a sprite sheet in swift and sprite kit? I've searched google, stack exchange, and the apple documentation and I can't figure this one out.
Asked
Active
Viewed 9,342 times
7
-
Thanks for the question, I was about to have this question myself. – Rob Norback Sep 17 '15 at 17:42
1 Answers
8
In xcode 7, you can use Images.xcassets to create a sprite atlas.
- Click on Images.xcassets
- Click on the plus bottom in the bottom left
- Select "new sprite atlas"
- Then you can drag and drop all your images into the atlas
If you drag in all three image sizes with the proper suffix (@2x and @3x) it will automatically populate the images for you.
Then to use these assets in code simply write:
let atlas = SKTextureAtlas(named: "Sprites")
let texture = atlas.textureNamed("ball")
let sprite = SKSpriteNode(texture: texture)
you can be less specific and just specify a texture:
let texture = SKTexture(imageNamed: "ball")
let sprite = SKSpriteNode(texture: texture)
If you're looking to use an already created sprite sheet, check out this answer on SO: Using sprite sheets in xcode.

Community
- 1
- 1

Rob Norback
- 6,401
- 2
- 34
- 38
-
-
2it looks like a way how to use multiple separate images like a spritesheet but doesn't answer the question - How to use spritesheets? – Vyachaslav Gerchicov Jan 09 '17 at 12:28
-
Can you be more specific with the information you're asking for @VyachaslavGerchicov? This question answers how to use a spritesheet in swift. They are referred to as Texture Atlases. – Rob Norback Jan 09 '17 at 23:43
-
2Example of spritesheet: http://flatredball.com/migrated_media/Cowsheet.png After your steps it will be imported as a single image. How to import it as a 16 separate images? To split them into separate images in the external editor is not an answer! – Vyachaslav Gerchicov Jan 10 '17 at 08:52
-
Looks like you might want this http://stackoverflow.com/questions/28008107/using-sprite-sheets-in-xcode, The answer is in swift even though it asks for objective-c. – Rob Norback Jan 10 '17 at 19:55
-
Lets say I have an image 2x and 3x, and within that image there are two button images with two states, one for normal and one for pressed. Is there any way to clip from that one image file, like it is possible in HTML!? – Famic Tech Jan 20 '18 at 15:59