如何在屏幕边缘制作排斥屏障?

时间:2017-06-02 04:45:35

标签: ios swift sprite-kit skfieldnode

func initializeBorederBlocks() {

    let upperBorderBlock: SKFieldNode = SKFieldNode.linearGravityField(withVector: vector3(0, -5, 0))

    upperBorderBlock.region = SKRegion(size: CGSize(width: self.frame.size.width, height: -10))
    upperBorderBlock.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height)
    upperBorderBlock.falloff = 2
    upperBorderBlock.strength = -100
    upperBorderBlock.categoryBitMask = PhysicsCategory.upperBorder.rawValue
//upperBorderBlock.isExclusive = true

    let lowerBorderBlock: SKFieldNode = SKFieldNode.linearGravityField(withVector: vector3(0, 5, 0))

    lowerBorderBlock.region = SKRegion(size: CGSize(width: self.frame.size.width, height: 10))
    lowerBorderBlock.position = CGPoint(x: self.frame.size.width / 2, y: 0)
    lowerBorderBlock.falloff = 2
    lowerBorderBlock.strength = -100
    lowerBorderBlock.categoryBitMask = PhysicsCategory.lowerBorder.rawValue
//lowerBorderBlock.isExclusive = true

    let leftBorderBlock: SKFieldNode = SKFieldNode.linearGravityField(withVector: vector3(5, 0, 0))

    leftBorderBlock.region = SKRegion(size: CGSize(width: 10, height: self.frame.size.height))
    leftBorderBlock.position = CGPoint(x: 0, y: self.frame.size.height / 2)
    leftBorderBlock.falloff = 2
    leftBorderBlock.strength = -100
    leftBorderBlock.categoryBitMask = PhysicsCategory.leftBorder.rawValue
//leftBorderBlock.isExclusive = true

    let rightBorderBlock: SKFieldNode = SKFieldNode.linearGravityField(withVector: vector3(-5, 0, 0))

    rightBorderBlock.region = SKRegion(size: CGSize(width: -10, height: self.frame.size.height))
    rightBorderBlock.position = CGPoint(x: self.frame.size.width, y: self.frame.size.height / 2)
    rightBorderBlock.falloff = 2
    rightBorderBlock.strength = -100
    rightBorderBlock.categoryBitMask = PhysicsCategory.rightBorder.rawValue
//rightBorderBlock.isExclusive = true

    self.addChild(upperBorderBlock)
    self.addChild(lowerBorderBlock)
    self.addChild(leftBorderBlock)
    self.addChild(rightBorderBlock)
}

通过此功能,我尝试初始化围绕屏幕边缘的四个SKFieldNode并向内施加一个力。这样,没有任何物体会粘在边缘上。然而,当我将一个物体运行到屏幕的边缘时,它只会坐在那里,感觉不到排斥力。我已将他们的categoryBitMasks添加到对象的collisionTestBitMask中,因此它知道要点击它们。此外,当节点数增加4时,它们就在现场。他们的位置,场强或矢量箭头是否有问题?

0 个答案:

没有答案