How to use a sprite sheet in swift and sprite kit?

女生的网名这么多〃 提交于 2019-12-03 09:19:59
Rob Norback

In xcode 7, you can use Images.xcassets to create a sprite atlas.

  1. Click on Images.xcassets
  2. Click on the plus bottom in the bottom left
  3. Select "new sprite atlas"
  4. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!