How to set the opacity of a tileset in spritekit tilemap?

257 views Asked by At

I have a code to define the PhysicsBody for everysingle tiles in tilemap like below:

func setUpSceneWithMap(map: SKTileMapNode) {

        let tileMap = map
        tileMap.setScale(0.4)

        let startingLocation: CGPoint = tileMap.position
        let tileSize = tileMap.tileSize

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

        for col in 0..<tileMap.numberOfColumns {

            for row in 0..<tileMap.numberOfRows {

                if  tileMap.tileDefinition(atColumn: col, row: row) != nil {

                    // defining different types of tile
                    let tileDefinition = tileMap.tileDefinition(atColumn: col, row: row)
                    ...
                    let isTrapTile = tileDefinition?.userData?["isTraps"] as? Bool

                    let x = CGFloat(col) * (tileSize.width) * 0.4 - halfWidth
                    let y = CGFloat(row) * (tileSize.height) * 0.4 - halfHeight

                    let rect = CGRect(x: 0, y: 0, width: tileSize.width * 0.4 , height: tileSize.height * 0.4)
                    let tileNode = SKShapeNode(rect: rect)

                    tileNode.position = CGPoint(x: x + startingLocation.x, y: y + startingLocation.y)
                    tileNode.zPosition = 1
                    tileNode.physicsBody = SKPhysicsBody(edgeLoopFrom: rect)

                    // Create the 4 checkpoint-tiles at the center of the maze
                    ...
                    // Create the wall tile
                    ...
                    // Create the trap tile
                    if (isTrapTile ?? false) {
                        tileNode.physicsBody?.categoryBitMask = gamePhysics.Trap
                        tileNode.physicsBody?.collisionBitMask = 0
                        tileNode.physicsBody?.contactTestBitMask = gamePhysics.Player
                        tileNode.physicsBody?.isDynamic = false
                    }

                    self.addChild(tileNode)

                }
            }
        }
    }

There are 3 different tile sets in my tilemap: Wall, CheckPoint and Trap. With the code above, i have assigned a Spritenode with physicsbody to all of them. Now, i want my Trap tile to be invisible so the Player cannot see it. What is the most efficient way to achieve it?

1

There are 1 answers

2
E. Huckabee On

You would use myNode.alpha = CGFloat;. This only affects nodes that inherit from SKNode.

According to the Apple Documentation on myNode.alpha value:

The default value is 1.0.

The SKNode class does not perform drawing, but many of its subclasses do. When a node or any of its descendants are drawn, the alpha component of each pixel is multiplied by the node’s alpha property and then clamped to the range 0.0-1.0. This modified alpha value is used to blend the pixel into the framebuffer

Basically, the alpha value affects how the sprite is rendered in the frame drawing.

In your case, you would set myTrapTileNode.alpha = 0.0; because you want it to be completely transparent.