2017-10-07 54 views
3

香港專業教育學院一直都在Apple Docs上GKNoiseMaps和我設法得到的圖像產卵,它在這裏使用此代碼SKTileMap正從GKNoiseMap

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 
    } 
} 

現在如何創建一個看起來非常好

與此更新代碼在這裏

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 
    } 
} 

SKTileMap,並與來自GKNoiseMap值產生SKTileMap作爲蘋果公司的文檔說明?

任何幫助,將不勝感激,因爲我真的不知道SKTileMaps以及它們是如何工作

回答

4

可以使用從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) 
} 
+0

好,謝謝,我會測試它。順便說一句我的今天推出的應用程序(它沒有任何與此有關)和其所謂的「蹩腳鴨」,我會很感激,如果你能檢查出來 –

+0

夥計!有用!但我需要根據噪聲級別來設置瓦片,我試圖根據噪聲級別創建地圖,因此地圖對它有一定的隨機性 –

+0

如果您使用init方法「public」初始化「SKTileGroup」 init(rules:[SKTileGroupRule])',那麼你可以指定一個'SKTileDefinitions'數組。這應該處理隨機性! –