我的目标是在 SKShapeNode 与另一个节点发生碰撞时立即更改该节点的 fillColor。我确实知道如何在碰撞点编辑物理体,但我无法弄清楚如何更改节点的填充或描边颜色等属性。
SKShapeNode:
func addBrick() -> SKShapeNode {
let brick = SKShapeNode(rect: CGRect(x: -100, y: -20, width: 200, height: 40), cornerRadius: 20)
brick.fillColor = .blue
brick.strokeColor = .blue
brick.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 200, height: 40))
brick.position = CGPoint(x: 0, y: -50)
brick.zPosition = 2
brick.physicsBody?.categoryBitMask = BrickCategory
brick.physicsBody?.collisionBitMask = PlayerCategory
brick.physicsBody?.contactTestBitMask = PlayerCategory
return brick
}
然后我测试玩家和积木之间的接触:
func didBegin(_ contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case PlayerCategory | BrickCategory:
print("")
default:
print("Unknown collision")
}
}
我知道我可以通过使用来改变物理体本身
contact.bodyB.node?.//make changes here
,但我不知道如何将 bodyB 的 fillColor 例如更改为红色。
我感谢您的帮助!