How do I make a repulsion barrier around the edge of my screen?

91 Views Asked by At
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)
}

Through this function, I am trying to initialize four SKFieldNodes that surround the edges of my screen and apply a force inwards. This way no objects will stick to the edges. However, when I do run an object into the edges of the screen, it will just sit there and feel no repulsion force. I have added their categoryBitMasks to the collisionTestBitMask of the object so it knows to hit them. Also, they are on the scene as the node count increases by four. Is there something wrong with their positionings, with their field strength, or their vector arrows?

0

There are 0 best solutions below