gpt4 book ai didi

swift - Sktilemapnode 从一张图片加载图 block

转载 作者:行者123 更新时间:2023-11-28 08:18:00 25 4
gpt4 key购买 nike

我有超过 1000 个磁贴需要使用。在平铺中,我只能使用所有图 block 的一张图片,但在 Xcode 中,我必须将图 block 表拆分为 1000 个单独的图 block 。有没有一种方法可以像在平铺中那样从一张图片中获取平铺?

最佳答案

如果您仍然需要它,我刚刚在 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

这是一个 20x24 block 16x16 像素的 spritesheet。

关于swift - Sktilemapnode 从一张图片加载图 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42057827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com