如何在didBegin接触函数内更改SKShapeNode的fillColor?

问题描述 投票:0回答:1

我的目标是,一旦该SKShapeNode与另一个Node碰撞,就立即更改它的fillColor。我确实知道如何在碰撞点编辑物理物体,但是我无法弄清楚如何更改节点的fill-或strokeColor之类的属性。

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更改为红色。

感谢您的帮助!

swift xcode sprite-kit skshapenode skphysicscontact
1个回答
1
投票

如果您有一个应该知道是SKNodenode SKShapeNode,则可以将其转换为:

if let shapeNode = node as? SKShapeNode {
    shapeNode.fillColor = .red
}
© www.soinside.com 2019 - 2024. All rights reserved.