在运行时更改SpriteNode PhysicsBody大小

时间:2015-07-09 16:43:17

标签: swift size skspritenode skphysicsbody

我希望能够在用户向下滑动时更改节点的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

1 个答案:

答案 0 :(得分:4)

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