Scene size in Xcode 6 / SpriteKit / Swift

I am trying to do something that should be very simple in Xcode, I am trying to add a sprite to the scene and sit in the lower left corner:

var groundTexture : SKTexture = SKTexture(imageNamed: "dirt-block") var ground : SKSpriteNode = SKSpriteNode(texture: groundTexture) ground.position = CGPointMake(ground.size.width/2, ground.size.height/2) self.addChild(ground) 

This shows nothing, so I looked at what the size of the scene is, and self.frame.size.width and self.frame.size.height return a width of 1024 and a height of 768, regardless of orientation.

I want this to be fixed in landscape mode, if that has any meaning for the answer?

Obviously, I missed a very fundamental idea related to setting up the scene, if someone could shed some light, that would be very grateful.

Thanks.

+6
source share
1 answer

You need to edit the GameViewController:

Cut everything from viewDidLoad() except super.viewDidLoad()

Overwrite viewWillLayoutSubviews

 override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { // Configure the view. var skView = self.view as SKView skView.showsFPS = true skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.size = skView.bounds.size scene.scaleMode = .AspectFill skView.presentScene(scene) } } 

You should also set the size of the scene, as you can see

 scene.size = skView.bounds.size 

Hope this helps

There is also a great tutorial where this is better explained: http://www.raywenderlich.com/49721/how-to-create-a-breakout-game-using-spritekit (Chapter 1 or 2)

+15
source

Source: https://habr.com/ru/post/976450/


All Articles