1

I'm trying to make my first game using SpriteKit & Swift. I think I understood the concept of World SKNode, but I can't make it work properly.

I add world:SKNode as a child to the GameScene. Then I add 9 child SKSpriteNodes (400px X 400px from starting from 200px X 200px point, because spritesNodes are centered). And they position perfectly, BUT I can't figure out how to get bounds of my world SKNode.

For example, how can I show left down corner of my world?

import SpriteKit

class GameScene: SKScene {
    let cityPartSide:Int = 400
    let startCoord:Int = 200

    var world:SKNode = SKNode()

    override func didMoveToView(view: SKView) {

        //self.world = SKNode()
        self.world.name = "world"
        self.addChild(self.world)
        let map:[[UInt8]] = [[16, 16, 16],
                             [16, 16, 16],
                             [16, 16, 16]]
        drawMap(world, map: map)

    }
    func drawMap(world:SKNode, map:[[UInt8]]) {
        let lineSize:Int = map[0].count
        var lineCounter = map.count
        while lineCounter > 0 {
            lineCounter -= 1
            var elemCounter = 0
            while elemCounter < lineSize {
                var sprite:SKSpriteNode = SKSpriteNode()
                let currentPartNumb = map[lineCounter][elemCounter]
                if currentPartNumb == 0 {
                    elemCounter += 1
                    continue
                } else {
                    sprite = SKSpriteNode(imageNamed: "cell")
                }
                //CHOOSE IMAGE
                sprite.position = CGPoint(x: startCoord + elemCounter * cityPartSide, y: startCoord + ((lineSize-1)-lineCounter)*cityPartSide)
                world.addChild(sprite)
                elemCounter += 1
            }
        }
    }
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

But what I get by running this code is that vertically it is positioned perfectly, but left side of the world is somewhere out of the screen.

enter image description here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Nikolay Pryahin
  • 377
  • 5
  • 19
  • Looks to me like you are not handling the scalingFactors correctly, how are you creating the scene itself? What scaleMode are you using? – Knight0fDragon Apr 07 '16 at 13:41

1 Answers1

1

Oh, that was quite easy. I needed to add this

scene.size = self.view.frame.size

in GameViewController.swift's viewDidLoad like this

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.
            let 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.scaleMode = .AspectFill
            scene.size = self.view.frame.size    //<---HERE
            skView.presentScene(scene)
        }
    }

    //...etc.
Nikolay Pryahin
  • 377
  • 5
  • 19