Reputation: 689
I have this game thats in landscape and I need a border around the whole screen so my heroNode can't go out of the screen on either of the four sides. I have this code but it only works for the iphone 5s and its too small for the other devices. How would I get the code to resize and fit the screen for the other devices? Thanks!
override func didMoveToView(view: SKView) {
let borderBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: 20, y: -90, width: self.size.width-50, height: self.size.height))
borderBody.categoryBitMask = borderbodycategory
borderBody.collisionBitMask = HeroCategory
borderBody.contactTestBitMask = HeroCategory
borderBody.allowsRotation = false
borderBody.affectedByGravity = false
self.physicsBody = borderBody
}
Upvotes: 0
Views: 1965
Reputation: 462
self.physicsBody = SKPhysicsBody (edgeLoopFromRect: self.frame)
This gives your "World" a physicsbody, which is an edge, a loop, the size of the "World"'s frame.
Upvotes: 6
Reputation: 7826
You can use UIScreen
's property instead of the fixed value.
let width = UIScreen.mainScreen().bounds.size.width - 50
let height = UIScreen.mainScreen().bounds.size.height - 50
let borderBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: 20, y: -90, width: width, height: height))
Upvotes: 1
Reputation: 1904
Instead of using a fixed value for your width, make use of percentage values so it scales with your screen size.
Upvotes: 2