0

I have a tileMapNode in which I want to assign a physics body to some tiles. I use the following function:

static func addPhysicsBody(to tileMap: SKTileMapNode, and tileInfo: String){

    let tileSize = tileMap.tileSize
    let halfWidth = CGFloat(tileMap.numberOfColumns) / 2 * tileSize.width
    let halfHeight = CGFloat(tileMap.numberOfRows) / 2 * tileSize.height

    for column in 0..<tileMap.numberOfColumns{
        for row in 0..<tileMap.numberOfRows{
            let tileDefinition = tileMap.tileDefinition(atColumn: column, row: row)
            let isCorrectTile = tileDefinition?.userData?[tileInfo] as? Bool
            if isCorrectTile ?? false && tileInfo == "wall"{
                let x = CGFloat(column) * 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.init(rectangleOf: tileSize, center: CGPoint(x: tileSize.width / 2, y: tileSize.height / 2))
                tileNode.physicsBody!.isDynamic = false
                tileNode.physicsBody!.restitution = 0.0
                tileNode.physicsBody!.categoryBitMask = Constants.PhysicsCategories.wall
                tileNode.physicsBody!.collisionBitMask = Constants.PhysicsCategories.player | Constants.PhysicsCategories.npc
                tileMap.addChild(tileNode)
            }
        }
    }
}

This all is working well, but if I run the app now, the tiles where I assigned a physics body to, have white borders. view.showphysics is set to false. Anyone an idea why there are white borders around the tiles? It looks like this:

enter image description here

Marcel
  • 472
  • 2
  • 6
  • 19
  • Your tileNode is a white square – Knight0fDragon Oct 05 '17 at 15:27
  • Yes, that was it.. I use tileNode.strokeColor = .clear and now it works, thanks! – Marcel Oct 05 '17 at 16:26
  • I dont understand Why you even have the SKShapeNode, you do not need it – Knight0fDragon Oct 05 '17 at 17:20
  • Are you creating invisible physics bodies? You may want to consider grouping them all into larger bodies and attaching it to a regular SKNode to improve performance. The SKShapeNode is not needed at all, unless you want to visually see the shape. – Knight0fDragon Oct 05 '17 at 17:21
  • That's a great tip! Yes I create invisible physics bodies. I will have a look at grouping bodies and use SKNodes instead of SKShapeNodes. Thanks! – Marcel Oct 06 '17 at 00:32

0 Answers0