Reputation: 121
I have over 1000 tiles that I need to use. In tiled I can use just one picture of all tiles but in Xcode I have to split the tilesheet in 1000 single tiles. Is there a way to make it possible to get the tiles from one picture like in tiled?
Upvotes: 1
Views: 486
Reputation: 423
If you still need it, I've just done it in a playground:
struct SpriteSheet {
let baseTexture: SKTexture
let rows: CGFloat
let columns: CGFloat
var textureSize: (width: CGFloat, height: CGFloat) {
return (width: baseTexture.textureRect().width / self.columns,
height: baseTexture.textureRect().height / self.rows)
}
init(imageNamed texture: String, rows: CGFloat, columns: CGFloat) {
self.baseTexture = SKTexture(imageNamed: texture)
self.baseTexture.filteringMode = .nearest // best for pixel art
self.rows = rows
self.columns = columns
}
var testTileMap: SKTileMapNode {
let tileSize: CGSize = CGSize(width: 16, height: 16)
let texture = cropTexture(row: 1, column: 10, w: 1, h: 1)
let tileDef = SKTileDefinition(texture: texture, size: tileSize)
let tileGroup = SKTileGroup(tileDefinition: tileDef)
let tileSet = SKTileSet(tileGroups: [tileGroup])
let mapNode = SKTileMapNode(tileSet: tileSet,
columns: 4,
rows: 4,
tileSize: tileSize,
fillWith: tileGroup)
return mapNode
}
func cropTexture(row: CGFloat, column: CGFloat, w: CGFloat, h: CGFloat) -> SKTexture {
assert(row < rows && column < columns && row > 0 && column > 0)
let rect = CGRect(x: textureSize.width * (column - 1),
y: textureSize.height * (self.rows - row),
width: textureSize.width * w,
height: textureSize.height * h)
return SKTexture(rect: rect, in: baseTexture)
}
}
let spriteSheet = SpriteSheet(imageNamed: "dungeon.png", rows: 24, columns: 20)
let node = spriteSheet.testTileMap
This is for a spritesheet of 20x24 tiles of 16x16 pixels.
Upvotes: 1
Reputation: 2307
You can use SKTiled or JSTilemap libraries to work with maps created in the Tiled Editor. If you want to use SpriteKits built in SKTilemapNode
then you need to use individual tiles.
Upvotes: 0