1

我正在尝试在最新版本的 Xcode 中制作一个简单的游戏。我使用非常简单的 TileMap。我试图弄清楚如何为每个瓷砖添加一个物理体。我被困在获取每个图块的位置并在那里添加一个 SKNode 。任何获取每个图块位置的方法都会非常有帮助。下面的所有代码都在 didMove(to: view) 中

let tileSize = grassTileMap.tileSize
let halfWidth = CGFloat(grassTileMap.numberOfColumns) / 2.0 * tileSize.width
let halfHeight = CGFloat(grassTileMap.numberOfRows) / 2.0 * tileSize.height

for node in self.children {
    if node.name == "grassTileMap" {
        grassTileMap = node as! SKTileMapNode
    }
    
    for col in 0..<grassTileMap.numberOfColumns {
        for row in 0..<grassTileMap.numberOfRows {
            let tileDef = grassTileMap.tileDefinition(atColumn: col, row: row)
            if tileDef == nil {
                print("no tile here")
            } else {
                
                let hitboxTileNode = SKSpriteNode(color: UIColor.clear, size: CGSize(width: 35.5, height: 35.5))
                
                // I need to set the position of each tile to hitBoxTileNode here
                
                hitboxTileNode.anchorPoint = CGPoint(x: 0, y: 0)
                hitboxTileNode.physicsBody = SKPhysicsBody(edgeLoopFrom: hitboxTileNode.frame)
                hitboxTileNode.physicsBody?.affectedByGravity = false
                hitboxTileNode.physicsBody?.isDynamic = false
                hitboxTileNode.physicsBody?.pinned = false
                hitboxTileNode.physicsBody?.restitution = 0
                hitboxTileNode.physicsBody?.friction = 0
                addChild(hitboxTileNode)
            }
        }
    }
}
4

0 回答 0