Search code examples
iossprite-kitskphysicsbody

physicsBody = SKPhysicsBody(edgeLoopFromRect: view.frame)


Trying to make my SKSpriteNode stay within the frame of a portrait only layout using edgeLoopFromRect: view.frame, and it only bounces off the left side of the frame and still leaves a gap....

Tried with "view", edgeLoopFromRect: frame, and it flew off the screen...

Any help would be appreciated..

func makeShip() {
    let ship = playerShip
    ship.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
    ship.physicsBody = SKPhysicsBody(rectangleOfSize: ship.frame.size)
    ship.physicsBody!.dynamic = true
    ship.physicsBody!.affectedByGravity = false
    ship.physicsBody!.mass = 0.08
    addChild(ship)
}


override func didMoveToView(view: SKView) {

    makeShip()
    physicsBody = SKPhysicsBody(edgeLoopFromRect: view.frame)
    motionManager.startAccelerometerUpdates()
}

Solution

  • In override func didMoveToView(view: SKView) try adding:

    let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    borderBody.friction = 0
    self.physicsBody = borderBody