So I am working on a game in SpriteKit. I have a function that generates a level for the player. The function uses images placed in assets.xcassets to generate levels. The game has multiple themes , namely fire water and electricity. Each theme has its own set of images with the same names but each theme image set is in its separate folder/group. Is it possible for me to programmatically tell the function to use a specific folder so that I can generate levels for other themes without renaming images. Or is it possible for me to use multiple asset folders and change which one the function uses at runtime. I want to use a random theme every time the player restarts the level.
Xcode simplifies managing most types of assets with asset catalogs. Use asset catalogs to organize and manage resources such as images, colors, app icons, textures, stickers, and data. Xcode also provides interactive editors for certain types of assets, like particle effects, that let you experiment, make changes, and see the results immediately.
Xcode provides tools and settings to help you add, organize, and optimize the different asset types your app uses. Xcode simplifies managing most types of assets with asset catalogs. Use asset catalogs to organize and manage resources such as images, colors, app icons, textures, stickers, and data.
Xcode also provides interactive editors for certain types of assets, like particle effects, that let you experiment, make changes, and see the results immediately. Add, organize, and edit sets of assets in your Xcode project using asset catalogs. Import images into your project, manage their appearances and variations, and load them at runtime.
Besides files, you can also add folders into the Xcode project follow the below steps. Right-click the Xcode project in the project navigator. Click Add Files to ” project name “ menu item in the popup menu list. Select a folder, check Copy items if needed checkbox in Destination section.
You can use a Sprite Atlas (also known as Sprite Sheet).
If you don't know what I'm talking about have a look at these 2 nice videos:
- Sprite Sheets - The Movie Part 1
- Sprite Sheets - The Movie Pt. 2 - Performance
If you have watched the videos now you know that the main target of a Sprite Atlas is loading into the GPU all and only (as far as possible) the images needed for the current scene or level. This way drawing each frame becomes much faster because all the resources al already on the GPU and the CPU only needs to transmit the coordinates.
Ok but how does it solve your problem?
Xcode allows you to create a namespace for each Sprite Atlas. So you can use the same name for resources in different Sprite Atlas.
This is pretty simple.
Asset Catalog
+
button you see in the image belowNew Sprite Atlas
5. Now click on the Provides Namespace
checkbox on the right.
That's it. Now just drop your images into the Sprites folder in order to add them to your sprite atlas.
In this example I'm going to rename the Sprite Atlas as Sprites0
and will add a red circle named circle
.
Then
Provides Namespace
checkboxSprites1
circle
.Here's the final result
Let's see now how to use it.
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let textureAtlas0 = SKTextureAtlas(named: "Sprites0")
let texture0 = textureAtlas0.textureNamed("circle")
let sprite0 = SKSpriteNode(texture: texture0)
sprite0.position.y = 100
addChild(sprite0)
let textureAtlas1 = SKTextureAtlas(named: "Sprites1")
let texture1 = textureAtlas1.textureNamed("circle")
let sprite1 = SKSpriteNode(texture: texture1)
sprite1.position.y = -100
addChild(sprite1)
}
}
Step 1: you load the Sprite Atlas
let textureAtlas0 = SKTextureAtlas(named: "Sprites0")
Step 2: you load a specific texture from the texture atlas
let texture0 = textureAtlas0.textureNamed("circle")
Step 3: you use your texture as you want
let sprite0 = SKSpriteNode(texture: texture0)
sprite0.position.y = 100
addChild(sprite0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With