coding22
coding22

Reputation: 689

How do I make a border around my whole screen for all the devices in Swift Xcode?

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

Answers (3)

Jake Crastly
Jake Crastly

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

Zigii Wong
Zigii Wong

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

Wraithseeker
Wraithseeker

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

Related Questions