【问题标题】:SKTileSet tile variant access in code代码中的 SKTileSet 平铺变体访问
【发布时间】:2016-10-27 23:25:08
【问题描述】:

我目前有一个 TileSet 有两个变体:Poison 和 NotPoison。我希望这些变体对被点击做出不同的反应,并且我还想遍历我的 TileMap 的各个行和列,以计算该行/列中“毒药”图块的数量。我以这种方式实现它是因为这种瓦片类型占用了瓦片地图的某个区域。如何访问代码中的特定变体?是否可以改变变体之间的行为?

【问题讨论】:

    标签: ios sprite-kit sktilemapnode


    【解决方案1】:

    您可以在 Tileset 中添加 userData,方法是单击图像 Poison 并创建一个带有键和值的 userData 项目,例如'isPoisonKey' 值为 1 和 dataType 布尔值。然后在你的gameScene.swift 代码中,你可以检查一个图块是否有这个用户数据。

    此示例遍历名为 backgroundSKTileMapNode 中的每一列/行,并在每次找到值为 true 的键 isPoisonKey 时打印到调试控制台

    var backgroundLayer: SKTileMapNode!
    
    override func didMove(to view: SKView) {
        guard let backgroundLayer = childNode(withName: "background") as? SKTileMapNode else {
            fatalError("Background node not loaded")
        }
    
        self.backgroundLayer = backgroundLayer
    
        for row in 0..<self.backgroundLayer.numberOfRows {
            for column in 0..<self.backgroundLayer.numberOfColumns {
                let backgroundTile = self.backgroundLayer.tileDefinition(atColumn: column, row: row)
                let isPoison = backgroundTile?.userData?.value(forKey: "isPoisonKey")
    
                if let countNode = isPoison as? Bool {
                    // Code here
                    if countNode {
                        print(countNode)
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 执行“self.backgroundLayer = backgroundLayer”时到底发生了什么
    • 这会将您在sks 文件中创建的SKTileMapNode 分配给一个变量,以便可以在swift 代码中访问它。
    猜你喜欢
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多