4

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.

Lukas Köhl
  • 1,569
  • 2
  • 15
  • 28
  • 1
    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 Nov 08 '15 at 11:16
  • 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 Nov 08 '15 at 11:18
  • @LukasKöhl and what is the actual size of a player when you print it out after its added to the scene ? – Whirlwind Nov 08 '15 at 11:19
  • 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 Nov 08 '15 at 11:24
  • @LukasKöhl Nice...Well giorashc was probably on the track to find the issue. So, you can thanks him :) – Whirlwind Nov 08 '15 at 11:57
  • The thanks was to everyone who replied. – Lukas Köhl Nov 08 '15 at 11:58

2 Answers2

5

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.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Already tried both and it didn't work. I also tried leaving the background away (no overlapping) and just add the player to the scene and he's still not visible although the image name is correct. Do you have another idea? – Lukas Köhl Nov 08 '15 at 08:47
  • Try to add extension along with image name.. Eg. "player.png" instead of "player". Or check if player node is positioned properly in the scene ... – Whirlwind Nov 08 '15 at 09:32
  • Sadly it doesn't make a difference. The coordinates are (31|31) with an anchor point at (0|0) (bottom left corner?) it should be visible and on the screen. – Lukas Köhl Nov 08 '15 at 09:56
  • Is node visible on screen depends on view and scene size. By default if creating scene from sks, its size is 1024x768 and that might differ from view's size based on device / simulator used for testing. Another question: are you using texture atlas or xcassets ? What I can think of next is to: clear derived data, clean project, clear simulator's content and settings and.re-install app on device. Also make sure that you don't have code logic problems or typos. – Whirlwind Nov 08 '15 at 10:58
  • I am using xcassets. Also I do not use the build in simulator because my Macbook is kinda slow so I run the programm directly on my iPhone. I am going to try 'reset' everything and try it again. If you want to I can give you my project via dropbox. – Lukas Köhl Nov 08 '15 at 11:12
  • @LukasKohl Yeah, an example project which can reproduce described behaviour might be useful in tracking down the issue. – Whirlwind Nov 08 '15 at 11:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94524/discussion-between-whirlwind-and-lukas-kohl). – Whirlwind Nov 08 '15 at 11:21
0

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

Manool
  • 11
  • 1
  • 1
  • Already tried that. Didn't work. I also tried leaving the background away and just add the player to the scene and he's still not visible. – Lukas Köhl Nov 08 '15 at 08:45