SKTileMapNode在视图中无法正确显示

时间:2018-09-27 20:18:31

标签: swift sprite-kit sktilemapnode

我正在以编程方式将切片添加到SKTileMapNode。通过获取正确数量的图块,一切都可以正常工作,只是在尝试将其显示在屏幕上时才出现问题。 Sample output 6x6 map

您会发现它看起来并不是真正的6x6,但是它应该更大一些,但是如果您查看图块的排列方式,则会在右上角看到5x5,然后相同颜色的大长条。但是现在,检查输出:

0.0 0 0 -0.24247201 1 0 -0.34696135 2 0 -0.42960075 3 0 -0.622688 4 0 -0.75301373 5 0 -0.1590038 0 1 -0.3293429 1 1 -0.4532277 2 1 -0.5414402 3 1 -0.6365295 4 1 -0.74014837 5 1 -0.19817059 0 2 -0.33490476 1 2 -0.5156779 2 2 -0.6816129 3 2 -0.74775416 4 2 -0.7614269 5 2 -0.232784 0 3 -0.38513133 1 3 -0.58570683 2 3 -0.7466342 3 3 -0.8207564 4 3 -0.8160349 5 3 -0.35956064 0 4 -0.4724255 1 4 -0.64021933 2 4 -0.7772983 3 4 -0.8449071 4 4 -0.8870924 5 4 -0.46846196 0 5 -0.53693193 1 5 -0.7096603 2 5 -0.8055851 3 5 -0.874175 4 5 -0.97741973 5 5

正好有36个图块,这意味着长条彩色条是一个图块。我该如何解决?

let surfaceTerrainSource:GKNoiseSource = GKPerlinNoiseSource(frequency: 5, octaveCount: 2, persistence: 0.1, lacunarity: 10, seed: Int32(arc4random()/2)) let surfaceTerrain:GKNoise = GKNoise(surfaceTerrainSource) /*let caveTerrainSource1:GKNoiseSource = GKPerlinNoiseSource(frequency: 4, octaveCount: 1, persistence: 0.1, lacunarity: 1.5, seed: 374) let caveTerrain:GKNoise = GKNoise(caveTerrainSource1) let caveTerrainSource2:GKNoiseSource = GKPerlinNoiseSource(frequency: 4, octaveCount: 1, persistence: 0.1, lacunarity: 1.5, seed: 128) let caveTerrain2:GKNoise = GKNoise(caveTerrainSource2) caveTerrain.multiply(caveTerrain2) surfaceTerrain.multiply(caveTerrain)*/ let terrainMap:GKNoiseMap = GKNoiseMap(surfaceTerrain) var terrainTiles:SKTileMapNode = SKTileMapNode() let tileSet = SKTileSet(named: "Sample Grid Tile Set")! let tileSize = CGSize(width: 8, height: 8) let rows = 6, cols = 6 terrainTiles = SKTileMapNode(tileSet: tileSet, columns: cols, rows: rows, tileSize: tileSize) //terrainTiles.enableAutomapping = true

    `for col in 0..<cols {
        for row in 0..<rows {
            //Get a value from our Noise Map returns -1.0 to 1.0
            let val = terrainMap.value(at: vector2(Int32(row),Int32(col)))
            //We will then decide what tiles correspond to what value
            print(val, " ", row, " ", col)
            switch val {
            case -1.0..<(-0.5):
                if let g = tileSet.tileGroups.first(where: {
                    ($0.name ?? "") == "Water"}) {
                    terrainTiles.setTileGroup(g, forColumn: col, row: row)
                }
            case -0.5..<(0.0):
                if let g = tileSet.tileGroups.first(where: {
                    ($0.name ?? "") == "Sand"}) {
                    terrainTiles.setTileGroup(g, forColumn: col, row: row)
                }
            case 0.0..<(0.5):
                if let g = tileSet.tileGroups.first(where: {
                    ($0.name ?? "") == "Grass"}) {
                    terrainTiles.setTileGroup(g, forColumn: col, row: row)
                }
            default:
                if let g = tileSet.tileGroups.first(where: {
                    ($0.name ?? "") == "Cobble"}) {
                    terrainTiles.setTileGroup(g, forColumn: col, row: row)
                }
            }
        }
    }
    //terrainTiles.anchorPoint = CGPoint(x: 0,y: 0)
    self.addChild(terrainTiles)
    print(terrainTiles.mapSize)`

如果有人可以帮助,这是我的代码

0 个答案:

没有答案