swift - enableAutomapping 和 SKTileMapNode : Create shapes with edges programmatically?

标签 swift sprite-kit sktilemapnode

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

enter image description here

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

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

这是我的代码(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)
}

最佳答案

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

我意识到在启用自动映射的情况下,您只需绘制形状的中心即可解决我的问题。边缘被填充并且 TileMapNode 的大小必须能够容纳边缘 block 。我必须让我的 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)
}

关于swift - enableAutomapping 和 SKTileMapNode : Create shapes with edges programmatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53909249/

相关文章:

ios - 如何在 Scene Kit 中更改场景?

swift - 在 SpriteKit 中的 TileMap 上连接 Physicsbodies

swift - 如何在 SpriteKit 中将一个图 block 更改为另一个图 block (或空白)?

ios - 从 Any 向下转换为协议(protocol)

ios - 在 Swift 中完成下载后调度?

ios - 当不在相机视野中时停止 SKVideoNode 播放

swift - 无法在另一个函数中调用 Swift 函数

ios - UserDefaults 不会调用 UIPickerView didSelect Int

xcode - ITMS-90289 - 在 Mac App Store 中使用应用程序组

ios - SpriteKit Tileset 内存泄漏