1
votes

I have a problem with positioning a sprite to correct point in a game scene. As far as I read position and anchorpoint and spritekit programming guide it should be clear that 0,0 is at the bottom left corner. I use landscape mode only.

But when I use my code:

import SpriteKit

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {
        var ground = createGround()
        self.addChild(ground)        
    }

    func createGround()->SKSpriteNode {
        var ground = SKSpriteNode(imageNamed: "ground.png")
        ground.name = "ground"
        ground.anchorPoint = CGPointMake(0, 0)
        ground.position = CGPointMake(0, 0)

        return ground
    }
}

And I implement scene pretty standard untouched:

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? 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

        skView.presentScene(scene)
    }
}

This is the result:

Screenshot of iPhone Simulator

My "ground.png" has a size of 120x120. I double-checked scenes anchor, it's at (0,0) and even when I set it in didMoveToView to (0,0), I've still same problem.

Somehow the Y factor is messed up. When I set ground position to (0,156) it's directly in the botton left corner. So it looks like game scenes y-anchor-point is -36 and anchor-point of ground is at top left instead of bottom left. Any clues why?

I found almost same problem but frame looks good with (0.0, 0.0, 1024.0, 768.0).

2
I found almost same problem but do not know if this is a good solution or if this would help me. Maybe you should try this solution to see if it helps you...Daniel Storm
It doesn't change anything. I checked self.frame and its (0.0, 0.0, 1024.0, 768.0). Problem must be somewhere else.Jurik
@Jurik That link you have posted has almost correct answer. Using the viewWillLayoutSubviews is good way if you check if(!skView.scene) before you initialize the scene, because this method can be called multiple times (ie when rotating device). In viewDidLoad, the final size of the view is not known yet, and that's why many developers use viewWillLayoutSubviews to initialize the scene. Can you update your question with how you initialize the scene in view controller currently ?Whirlwind
@Whirlwind done! But what do you mean with viewWillLayoutSubviews? Because override func viewWillLayoutSubviews() doesn't work ... sorry, I'm REALLY new to this stuff.Jurik
What do you mean by "not working" ? If you provide me with example of your code, maybe I could tell you if everything is okay there...Whirlwind

2 Answers

0
votes

sprite anchor point of (.5,.5) means it will be positioned via its center when placed. So if it's placed at the bottom left point on the screen, you will only see the top half, and the right half of the image.

If you want an image to fit perfectly in the bottom left corner without additional math, sets its anchor point to:

sprite.anchorPoint = CGPointMake(0, 1)
0
votes

This should help.

func createGround()->SKSpriteNode {
    var ground = SKSpriteNode(imageNamed: "ground.png")
    ground.name = "ground"
//  ground.anchorPoint = CGPointMake(0, 0) leave this at .5,.5
    ground.position = CGPointMake(ground.size.width/2, ground.size.height/2)

    return ground
}

And change your scene scale.

scene.scaleMode = .AspectFit

If your scene is 1024 by 768 and set to AspectFill some of the scene will get cutoff and it will complicate things. For best results set your scene size to something that will fit nicely on an iPhone, but for the short term Aspect fit will get the entire scene on the screen. Either way you will either have some of it get cut off or have black bars until you change your scene size to something like 736 by 414 (iPhone 6+)

Because the origin of the ground is in the center by default and the origin of the scene is on the bottom left you need to move the position over by half the width and height of the ground.

Hopefully that helps and makes sense.