4
votes

I want to create my player on top of the background. Somehow this doesn't work. The node counter goes up by one but it isn't visible. I do add the player after I add the background so it should be on top. The code I use to create the player:

var player = SKSpriteNode()

func addPlayer(gameScene: GameScene, xPos: CGFloat, yPos: CGFloat){
    player = SKSpriteNode(imageNamed: "player")
    player.size = CGSize(width: fieldsWidth, height: fieldsWidth)
    player.position = CGPoint(x: xPos, y: yPos)
    gameScene.addChild(player)
}

And I access the function from another class like this:

        person().addPlayer(self, xPos: fieldsWidth/2, yPos: fieldsWidth/2)

Hopefully you can help me.

2
why do explicitly set the size of the sprite? try removing the size line or check that the values of fieldsWidth and fieldsHeight are valid (also note that for the height you use fieldsWidth) - giorashc
I set the size of the sprite because of the size of the phone. The sprite should not have the same size on an iPhone 6 and on an iPhone 6s. I calculate a specific ratio. - Lukas Köhl
@LukasKöhl and what is the actual size of a player when you print it out after its added to the scene ? - Whirlwind
Oh man I found the mistake. So much trouble for such a silly mistake. fieldsWidth was set to 0 so the node could not be visible but shown in the node counter. Thank you for your time. - Lukas Köhl
@LukasKöhl Nice...Well giorashc was probably on the track to find the issue. So, you can thanks him :) - Whirlwind

2 Answers

4
votes

If you are sure that you have added node at desired position, which is probably the situation here because node count is incremented, you can set player's and background's zPosition to make player above the background:

var player = SKSpriteNode()

//1. somewhere in your code set background's zPosition to 1 before you add it to the scene

func addPlayer(gameScene: GameScene, xPos: CGFloat, yPos: CGFloat){
    player = SKSpriteNode(imageNamed: "player")

    //2. Set player's zPosition to be higher than background's zPosition
    player.zPosition = 2 
    player.size = CGSize(width: fieldsWidth, height: fieldsWidth)
    player.position = CGPoint(x: xPos, y: yPos)
    gameScene.addChild(player)
}

About ignoresSiblingOrder...I would recommend you to leave that to true because it can help a lot when it comes to performance . The only "thing" about this kind of optimization is that you have to explicitly set zPosition for nodes at same zPosition which can overlap, but you don't want to let them overlap in random order (which ignoresSiblingsOrder does when set to true), but rather to overlap in determined order (controlled by you). This is from docs:

When this property is set to YES, the position of the nodes in the tree is ignored when determining the rendering order. The rendering order of nodes at the same z position is arbitrary and may change every time a new frame is rendered.

So, just set zPosition explicitly and you will be fine.

0
votes

In the View Controller of your scene, set skView.ignoresSiblingOrder = false.