【问题标题】:enableAutomapping and SKTileMapNode: Create shapes with edges programmatically?enableAutomapping 和 SKTileMapNode:以编程方式创建带边缘的形状?
【发布时间】:2018-12-24 04:45:07
【问题描述】:

我正在尝试以编程方式为 RPG 风格游戏中的对话框文本创建一个灵活的对话框。我设计了一个简单的方框图形,将其划分为所有正确的部分,并将其作为 8 路邻接组添加到我的图块集中。

我可以成功地将它添加到我的相机节点,但自动映射不起作用。我得到的只是一个由中心瓷砖组成的纯黑盒子。根据Apple's documentation,enableAutomapping“指定瓦片地图是否像场景编辑器一样使用自动映射行为”,所以我相信这个设置应该对我有帮助。

它只选择中心瓷砖,忽略所有边缘/角落,我得到一个纯黑框。我正在寻找的行为是让 SpriteKit 根据我正在创建的形状选择合适的边缘和内部瓷砖。

这是我的代码 (Swift 4.2) 目前的样子:

func createDialogBox() {
    let dialogTileSet = SKTileSet(named: "Dialog")!

    for tileGroup in dialogTileSet.tileGroups {
        for tileRule in tileGroup.rules {
            for tileDefinition in tileRule.tileDefinitions {
                for texture in tileDefinition.textures {
                    texture.filteringMode = .nearest
                }
            }
        }
    }

    let tileSize = CGSize(width: 32, height: 32)
    let rows = 3, cols = 12

    let dialogBox = SKTileMapNode(tileSet: dialogTileSet,
                                  columns: cols,
                                  rows: rows,
                                  tileSize: tileSize)

    dialogBox.enableAutomapping = true

    for col in 0..<cols {
        for row in 0..<rows {
            dialogBox.setTileGroup(dialogTileSet.tileGroups.first, forColumn: col, row: row)
        }
    }

    dialogBox.zPosition = 2
    dialogBox.position = CGPoint(x: 48, y: -128)
    self.camera?.addChild(dialogBox)
}

【问题讨论】:

    标签: swift sprite-kit sktilemapnode


    【解决方案1】:

    以编程方式使用 SpriteKit 和 SKTileMapNodes 的建议:在尝试在代码中执行之前了解编辑器在 Xcode 中的工作方式。问了我的问题后,我决定在编辑器中构建它,我意识到我的瓦片地图节点的概念模型是不正确的。

    我意识到启用自动映射后,我能够解决我的问题,您只需要绘制形状的中心即可。边缘被填充,TileMapNode 的大小必须能够容纳边缘块。我不得不让我的 TileMapNode 稍微大一点。

    现在可以了。这是代码(Swift 4.2):

    func createDialogBox(string: String) {
        let dialogTileSet = SKTileSet(named: "Dialog")!
    
        for tileGroup in dialogTileSet.tileGroups {
            for tileRule in tileGroup.rules {
                for tileDefinition in tileRule.tileDefinitions {
                    for texture in tileDefinition.textures {
                        texture.filteringMode = .nearest
                    }
                }
            }
        }
    
        let tileSize = CGSize(width: 32, height: 32)
        let rows = 5, cols = 14 // Increased the size of the tile map node.
    
        let dialogBox = SKTileMapNode(tileSet: dialogTileSet,
                                      columns: cols,
                                      rows: rows,
                                      tileSize: tileSize)
    
        dialogBox.enableAutomapping = true
    
        // Just draw the center line. Automapping will take care of the surrounding edges.
        for col in 2...11 {
            dialogBox.setTileGroup(dialogTileSet.tileGroups.first, forColumn: col, row: 2)
        }
    
        dialogBox.zPosition = 2
        dialogBox.position = CGPoint(x: 48, y: -128)
        self.camera?.addChild(dialogBox)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2023-03-24
      相关资源
      最近更新 更多