在 运行 时间更改 SkspriteNode PhysicsBody 大小

Change SpriteNode PhysicsBody Size at Run Time

我希望能够在用户向下滑动时更改节点的 physicsBody 高度,但除了重置整个 physicsBody 之外还没有找到如何执行此操作的方法。

当我最初加载节点时,我使用以下代码:

    nodeHero.color = UIColor .grayColor()
    nodeHero.size.width = 20
    nodeHero.size.height = 45
    nodeHero.position.x = -frame.size.width/2 + 45
    nodeHero.position.y = pointMainY + 30 + nodeHero.size.height/2
    nodeHero.zPosition = 110

    nodeHero.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(nodeHero.size.width, nodeHero.size.height))
    nodeHero.physicsBody?.mass = 1
    nodeHero.physicsBody?.angularVelocity = 0
    nodeHero.physicsBody?.allowsRotation = false
    nodeHero.physicsBody?.restitution = 0
    nodeHero.physicsBody?.categoryBitMask = bitHero

    addChild(nodeHero)

当我向下滑动时,我希望能够做这样的事情(这行不通):

    nodeHero.size.height = 28
    nodeHero.physicsBody?.size.height = 28

但是我必须再次使用 nodeHero.physicsBody = SKPhysicsBody(),这会重置所有其他 physicsBody 属性,所以我必须这样做:

    nodeHero.size.height = 28

    nodeHero.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(nodeHero.size.width, nodeHero.size.height))
    nodeHero.physicsBody?.mass = 1
    nodeHero.physicsBody?.angularVelocity = 0
    nodeHero.physicsBody?.allowsRotation = false
    nodeHero.physicsBody?.restitution = 0
    nodeHero.physicsBody?.categoryBitMask = bitHero

According to SpriteKit documentation SKPhysicsBody 的区域无法修改,因此您需要创建另一个 SKPhysicsBody 实例并从上一个实例中复制要保留的值。