简体   繁体   English

Xcode-Swift SpriteKit:具有“物理空白”的SKTileMapNode

[英]Xcode - Swift SpriteKit: SKTileMapNode with a “physics gap”

I am playing around with SpriteKit and the TileMapNode and i've got an annoying problem. 我在玩SpriteKit和TileMapNode,遇到了一个烦人的问题。

That's how it should look like. 这就是它的外观。 在此处输入图片说明

That's how its actually looking in the simulator/device. 这就是它在模拟器/设备中的实际外观。

在此处输入图片说明

The white spaces are terrible and i have no idea, how to get rid of them. 这些空白非常糟糕,我不知道如何消除它们。 My Tiles are about 70x70 in the "Sprite Atlas - Part" of the assets, i've configured my tilemapnode with a scale of 0.5 and tile size of 70x70. 我的图块在资产的“ Sprite Atlas-Part”中约为70x70,我已将我的tilemapnode配置为0.5的比例和70x70的图块大小。

While testing some cases, i figured out that this part of code triggers the error, but i have no idea, what could be wrong. 在测试某些情况时,我发现这部分代码会触发错误,但是我不知道这可能是什么错误。 Changing the SKPhysicsBody size to a smaller one, did not helped. SKPhysicsBody大小更改为SKPhysicsBody大小并没有帮助。

guard let tilemap = childNode(withName: "LevelGround") as? SKTileMapNode else { return }            
let tileSize = tilemap.tileSize
        let halfWidth = CGFloat(tilemap.numberOfColumns) / 2.0 * tileSize.width
        let halfHeight = CGFloat(tilemap.numberOfRows) / 2.0 * tileSize.height
        for row in 0..<tilemap.numberOfRows {
            for col in 0..<tilemap.numberOfColumns {
                if tilemap.tileDefinition(atColumn: col, row: row) != nil {
                    let x = CGFloat(col) * tileSize.width - halfWidth
                    let y = CGFloat(row) * tileSize.height - halfHeight
                    let rect = CGRect(x: 0, y: 0, width: tileSize.width, height: tileSize.height)
                    let tileNode = SKShapeNode(rect: rect)
                    tileNode.position = CGPoint(x: x, y: y)
                    tileNode.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 70, height: 70), center: CGPoint(x: tileSize.width / 2.0, y: tileSize.height / 2.0))
                    tileNode.physicsBody?.isDynamic = false
                    tileNode.physicsBody?.collisionBitMask = 2
                    tileNode.physicsBody?.categoryBitMask = 1
                    tileNode.physicsBody?.contactTestBitMask = 2 | 1
                    tileNode.name = "Ground"
                    tilemap.addChild(tileNode)
                }
            }
        }
tileNode.strokeColor = .clear

solved the problem 解决了问题

Update: problem not solved... just moved :/ 更新:问题没有解决...只是感动:/

When checking, if ground & player are in contact, every new tile the status is switching between "contact" and "no contact". 检查时,如果地面和播放器处于接触状态,则每个新图块的状态都会在“接触”和“无接触”之间切换。 When using a cube instead a circle, the cube begins to rotate. 当使用立方体而不是圆形时,立方体开始旋转。 It seems, the corner of the cube get's stuck at the minimal space between the tiles. 看来,立方体的一角卡在了瓷砖之间的最小空间上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM