SKTileMap 从 GKNoiseMap 获取值
Posted
技术标签:
【中文标题】SKTileMap 从 GKNoiseMap 获取值【英文标题】:SKTileMap getting Values from GKNoiseMap 【发布时间】:2017-10-07 08:34:37 【问题描述】:我一直在 GKNoiseMaps 上关注Apple Docs,我设法生成了一张图片,它看起来非常好
在此处使用此代码
class GameScene: SKScene
class Noise: GKNoise
var NoiseSource = GKPerlinNoiseSource(frequency: 0.05, octaveCount: 3, persistence: 1, lacunarity: 1, seed: Int32(arc4random_uniform(UInt32(500 - 1))))
override init(_ noiseSource: GKNoiseSource, gradientColors: [NSNumber : UIColor])
super.init(NoiseSource, gradientColors: [ (+1.0 as NSNumber): UIColor.red, (-1.0 as NSNumber) : UIColor.black])
let noise = Noise()
let Vector1 = vector_double2(1.0, 1.0)
override func didMove(to view: SKView)
let NoiseMap = GKNoiseMap(noise, size: vector_double2(300.0, 300.0),
origin: vector_double2(0.0, 0.0),
sampleCount: vector_int2(100),
seamless: true)
let texture = SKTexture(noiseMap: NoiseMap)
let Node = SKSpriteNode(texture: texture)
Node.size = CGSize(width: 1000,height: 1000)
Node.position = CGPoint(x: 0,y: 0)
self.addChild(Node)
override func update(_ currentTime: TimeInterval)
// Called before each frame is rendered
现在我如何在此处使用此更新的代码创建 SKTileMap
class GameScene: SKScene
class Noise: GKNoise
var NoiseSource = GKPerlinNoiseSource(frequency: 0.05, octaveCount: 3, persistence: 1, lacunarity: 1, seed: Int32(arc4random_uniform(UInt32(500 - 1))))
override init(_ noiseSource: GKNoiseSource, gradientColors: [NSNumber : UIColor])
super.init(NoiseSource, gradientColors: [(+1.0 as NSNumber): UIColor.red, (-1.0 as NSNumber) : UIColor.black])
let noise = Noise()
let Vector1 = vector_double2(1.0, 1.0)
override func didMove(to view: SKView)
let NoiseMap = GKNoiseMap(noise, size: vector_double2(300.0, 300.0),
origin: vector_double2(0.0, 0.0),
sampleCount: vector_int2(100),
seamless: true)
let tileGroup = [SKTileGroup]()
let tileSet = SKTileSet(tileGroups: tileGroup)
let map = SKTileMapNode(tileSet: tileSet, columns: 10, rows: 10, tileSize: CGSize(width: 20,height: 20), tileGroupLayout: tileGroup)
override func update(_ currentTime: TimeInterval)
// Called before each frame is rendered
并使用 GKNoiseMap 中的值生成 SKTileMap,如 Apple Docs 中所述?
任何帮助将不胜感激,因为我真的不了解 SKTileMaps 及其工作原理
【问题讨论】:
【参考方案1】:您可以使用从NoiseMap
创建的纹理创建SKTileDefinition
。然后可以在任何位置将此图块绘制到SKTileMapNode
中。此示例遍历所有列和行并设置图块。我将 NoiseMap 的大小设置为 64 x 64,因为这是平铺的典型大小。
override func sceneDidLoad()
let noise = Noise()
let noiseMap = GKNoiseMap(noise, size: vector_double2(64.0, 64.0), origin: vector_double2(0.0, 0.0), sampleCount: vector_int2(100), seamless: true)
let bgTexture = SKTexture(noiseMap: noiseMap)
let bgDefinition = SKTileDefinition(texture: bgTexture, size: bgTexture.size())
let bgGroup = SKTileGroup(tileDefinition: bgDefinition)
bgGroup.name = "noiseTest"
let tileSet = SKTileSet(tileGroups: [bgGroup])
let bgNode = SKTileMapNode(tileSet: tileSet, columns: 10, rows: 10, tileSize: bgTexture.size())
let tile = bgNode.tileSet.tileGroups.first(where: $0.name == "noiseTest" )
for column in 0 ..< bgNode.numberOfColumns
for row in 0 ..< bgNode.numberOfRows
bgNode.setTileGroup(tile, forColumn: column, row: row)
bgNode.position = CGPoint(x: 0, y: 0)
bgNode.setScale(1)
self.addChild(bgNode)
【讨论】:
好的,谢谢,我会测试它。顺便说一句,我今天推出的应用程序(与此无关),它的名称为“Crappy Duck”,如果您能查看一下,我将不胜感激 老兄!有用!但我需要根据噪音水平设置图块我试图根据噪音水平创建地图,所以地图有一点随机性 如果您使用初始化方法public init(rules: [SKTileGroupRule])
初始化SKTileGroup
,那么您可以指定SKTileDefinitions
的数组。那应该处理随机性!
你能用一个例子更新你的答案,这样我们就不会因为不是正确答案而惹上麻烦
我认为随机化 SKTileMapNode 是另一回事。你可以自己试试,然后再问一个问题。以上是关于SKTileMap 从 GKNoiseMap 获取值的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 中将 SKTilemap 场景定位在屏幕中间