gurehbgui
gurehbgui

Reputation: 14694

Get bounds of the screen in SKScene - didMoveToView(...)

I'm new to SpriteKit and Swift, I have the following question:

I start a new SpriteKit Swift project and then open the

class GameScene: SKScene

there is the method

override func didMoveToView(view: SKView)

Now I want to create a worldBorder which is exactly the same size like the iphone screen (landscape or portrait) It should be like the bounds within e.g. a ball can not get out.

I did something like this:

let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = worldBorder

But this is not working, its bigger then the part which is dieplayed on the phone.

How to make this?

EDIT: My GameScene has this properties and the heigh of my iphone work as a border. but in portraite mode the bounds are to much left and right. enter image description here

Upvotes: 0

Views: 267

Answers (2)

TerranceW
TerranceW

Reputation: 286

It is possible that due to the fact worldBorder is a PhysicsBody, it is being affected by gravity and falling down past the screen. Try pinning it:

worldBorder.physicsBody?.pinned = true

Another option is to just create several nodes and position them at the border surrounding the frame.

Upvotes: 1

Manav Gabhawala
Manav Gabhawala

Reputation: 997

Try doing this:

override func didMoveToView(view: SKView) {
    let worldBorder = SKPhysicsBody(edgeLoopFromRect: view.frame)
    self.physicsBody = worldBorder
}   

I think maybe the self.frame hasn't been resized when didMoveToView is called. Also, if you support multiple orientations like portrait and landscape you might need to handle transitioning to the other size.

Upvotes: 0

Related Questions