The following code recognizes the bottom and top edges of the scene and the ball bounces off as expected. However, the left and right edges of the scene are breached all the time. The ball goes off screen and then eventually returns back if enough force is applied. It is as if the edges of the scene are beyond the edges of the iphone simulator window.
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView){
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
backgroundColor = UIColor.cyanColor()
var ball = SKSpriteNode(imageNamed: "ball")
ball.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.height/2)
let push = CGVectorMake(10, 10)
ball.physicsBody.applyImpulse(push)
}
}
I think it has something to do with the .sks file since dimensions are set there and if I change those dimensions it reflects on the game. Anyone know how to make it so this line of code takes precedence over the .sks file dimensions? This way it will be dynamic and could work on different devices.
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
I found the same question here but was never answered: Swift physicsbody edge recognition issue
Also, I am new to iOS app dev and swift so forgive me if the answer is obvious and I missed it.