简体   繁体   English

如何在 didBegin 联系人 function 中更改 SKShapeNode 的填充颜色?

[英]How to change the fillColor of a SKShapeNode within the didBegin contact function?

My goal is to change the fillColor of a SKShapeNode as soon as that node collides with another Node.我的目标是在 SKShapeNode 与另一个节点发生碰撞时立即更改该节点的 fillColor。 I do know how to edit the physics body at the point of collision but I couldn't manage to figure out how to change properties like fill- or strokeColor of a Node.我确实知道如何在碰撞点编辑物理体,但我无法弄清楚如何更改节点的填充或描边颜色等属性。

The SKShapeNode: 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
}

Then I test the contact between the player and the brick:然后我测试玩家和积木之间的接触:

    func didBegin(_ contact: SKPhysicsContact) {
    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch contactMask {
    case PlayerCategory | BrickCategory:
        print("")



    default:
        print("Unknown collision")
    }
}

I do know that I can make changes to the physics body itself by using我知道我可以通过使用来改变物理体本身

contact.bodyB.node?.//make changes here

, but I don't know how to change the fillColor of bodyB for example to red. ,但我不知道如何将 bodyB 的 fillColor 例如更改为红色。

I appreciate your help!我感谢您的帮助!

If you have an SKNode node that you know should be an SKShapeNode , then you can cast it like:如果您有一个您知道应该是SKShapeNodeSKNode node ,那么您可以将其转换为:

if let shapeNode = node as? SKShapeNode {
    shapeNode.fillColor = .red
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM