真实设备上的SKTileMap物理错误

时间:2019-03-27 17:03:47

标签: swift xcode sprite-kit skspritenode sktilemapnode

我一直在开发利用瓷砖地图的2D SpriteKit游戏。我使用此功能向瓷砖地图添加了SKPhysicsBody

import SpriteKit

func giveTileMapPhysicsBody(map: SKTileMapNode) {
    let tileMap = map
    let tileSize = tileMap.tileSize
    let halfWidth = CGFloat(tileMap.numberOfColumns) / 2.0 * tileSize.width
    let halfHeight = CGFloat(tileMap.numberOfRows) / 2.0 * tileSize.height

    for col in 0..<tileMap.numberOfColumns {

        for row in 0..<tileMap.numberOfRows {

            if let tileDefinition = tileMap.tileDefinition(atColumn: col, row: row)

            {

                //let isEdgeTile = tileDefinition.userData?["AddBody"] as? Int      //uncomment this if needed, see article notes
                //if (isEdgeTile != 0) {
                let tileArray = tileDefinition.textures
                let tileTexture = tileArray[0]
                let x = CGFloat(col) * tileSize.width - halfWidth + (tileSize.width/8)
                let y = CGFloat(row) * tileSize.height - halfHeight + (tileSize.height/8)
                _ = CGRect(x: 0, y: 0, width: tileSize.width, height: tileSize.height)
                let tileNode = SKNode()

                tileNode.position = CGPoint(x: x, y: y)
                tileNode.physicsBody = SKPhysicsBody(texture: tileTexture, size: CGSize(width: (tileTexture.size().width), height: (tileTexture.size().height)))
                tileNode.physicsBody?.linearDamping = 0.8 //60.0
                tileNode.physicsBody?.affectedByGravity = false
                tileNode.physicsBody?.allowsRotation = false
                tileNode.physicsBody?.restitution = 0.2
                tileNode.physicsBody?.isDynamic = false


                tileNode.physicsBody?.categoryBitMask = PhysicsCategory.tileMap
                tileNode.physicsBody?.contactTestBitMask = PhysicsCategory.player
                tileNode.physicsBody?.collisionBitMask = PhysicsCategory.player
                tileNode.physicsBody?.friction = 0.8 // 3.0

                tileMap.addChild(tileNode)
                //}
            }
        }
    }
}

您可以在第一个屏幕截图中看到,当我在模拟器上运行游戏时,一切正常,但是当我在真实的iPhone或iPad上运行游戏时,平铺贴图物理存在一个错误(第二张图片)。正如您可以在第二张图像上看到的那样,在我的真实设备上运行该图像时存在物理间隙。 你知道会是什么吗?

enter image description here

enter image description here

编辑:

不幸的是,它不适用于我的项目。物理形状与地图不正确匹配。 enter image description here

1 个答案:

答案 0 :(得分:0)

尝试像这样实现它

typealias TileCoordinates = (column: Int, row: Int)

func giveTileMapPhysicsBody(map: SKTileMapNode)  {
    let tileMap = map

    var physicsBodies = [SKPhysicsBody]()

    for row in 0..<tileMap.numberOfRows {
        for column in 0..<tileMap.numberOfColumns {
            guard let tile = tile(in: tileMap, at: (column, row)) else { continue }

            let center = tileMap.centerOfTile(atColumn: column, row: row)
            let body = SKPhysicsBody(rectangleOf: tile.size, center: center)
            physicsBodies.append(body)
        }
    }

    tileMap.physicsBody = SKPhysicsBody(bodies: physicsBodies)
    tileMap.physicsBody?.categoryBitMask = PhysicsCategory.tileMap
    tileMap.physicsBody?.contactTestBitMask = PhysicsCategory.player
    tileMap.physicsBody?.collisionBitMask = PhysicsCategory.player
    tileMap.physicsBody?.linearDamping = 0.8 //60.0
    tileMap.physicsBody?.affectedByGravity = false
    tileMap.physicsBody?.allowsRotation = false
    tileMap.physicsBody?.restitution = 0.2
    tileMap.physicsBody?.isDynamic = false
    tileMap.physicsBody?.friction = 0.8 // 3.0
}

func tile(in tileMap: SKTileMapNode, at coordinates: TileCoordinates) -> SKTileDefinition? {
    return tileMap.tileDefinition(atColumn: coordinates.column, row: coordinates.row)
}