Tiling a sprite texture in SpriteKit
I'm writing my next game, Icarus - Escape from Crete, using Xamarin iOS together with SpriteKit. The combination works great and SpriteKit turns out to be a well thought out API. My only gripe so far is that I haven't found an existing way for tiling a sprite texture, that is fill the entire sprite using a smaller texture.
The grass below is just a small image, repeated to fill the entire green meadow.
The grass is created from this simple tile
using an extension method on SKSpriteNode
Using CoreGraphics, an image of the proper size is created and the texture is tiled. Next the image is rendered and a SKTexture
is created from the result.
Here's the example from Icarus:
This method has two disadvantages though.
- It's slow. Create the needed nodes during game loading, not during gameplay.
- The resulting image is flipped. Remedy this by either using a flipped image as the tile, or flip the Y-axis on the
SKSpriteNode
after the texture is applied:grass.YScale = -1f;
I use the former.
Even with the disadvantages, the method was useful in Icarus. Maybe it's useful for you too 😃