我有一个代码,可以为tilemap中的每个单个瓦片定义PhysicsBody,如下所示:
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)
}
}
}
}
我的tilemap中有3种不同的tile集:Wall,CheckPoint和Trap。通过上面的代码,我为所有对象分配了一个带有Physicalbody的Spritenode。现在,我希望陷阱瓷砖不可见,以便玩家看不到它。最有效的方法是什么?
myNode.alpha = CGFloat;
。这仅影响从SKNode继承的节点。