0

Any ideas as to why my Sprites aren't appearing in the correct order here? The snowflake shows up at index 0 instead of 1.

Even if I switch the order around, the snowflake still appears behind everything else.

import Foundation
import SpriteKit

class page1: SKScene {
    let background = SKSpriteNode(imageNamed: "01_BG")
    let snowflake = SKSpriteNode(imageNamed: "home_snowflake02")
    let room = SKSpriteNode(imageNamed: "01_room")
    let kid = SKSpriteNode(imageNamed: "01_kidStanding")

    override init(size: CGSize){
        super.init(size: size)
    }

    override func didMoveToView(view: SKView) {
        //Set up background
        background.position = CGPoint(x: 200, y: 380)
        background.xScale = 1.0
        background.yScale = 1.0
        insertChild(background, atIndex: 0)

        //Set up Snowflakes
        snowflake.position = CGPoint(x: 250, y: 1100)
        snowflake.xScale = 1.0
        snowflake.yScale = 1.0
        insertChild(snowflake, atIndex: 1)

        //Set up room
        room.position = CGPoint(x: 512, y: 384)
        room.xScale = 1.0
        room.yScale = 1.0
        insertChild(room, atIndex: 2)

        //Set up kid
        kid.position = CGPoint(x: 320, y: 290)
        kid.xScale = 1.0
        kid.yScale = 1.0
        insertChild(kid, atIndex: 3)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
jwade502
  • 898
  • 1
  • 9
  • 22

1 Answers1

0

If your scenes view has it's ignoresSiblingOrder property set to true, performance can be improved but the order of sibling sprites is unreliable.

You can set a sprites zPosition directly to deal with this.

eg:

background.zPosition = 0
snowflake.zPosition = 1

or

snowflake.zPosition = background.zPosition + 1

to keep it relative to a sibling.

Nightly
  • 581
  • 3
  • 10
  • Thank you for showing me this, but I have found the real problem! In the default View controller in SpriteKit, ignoresSiblingOrder is set to true, so the compiler was ignoring my layer assignments! Found it in this thread http://stackoverflow.com/questions/21709235/spritekit-not-respecting-zposition – jwade502 Jan 27 '15 at 17:58
  • The question you linked is correct, but it's more specifically dealing with nodes with different parents. All of your nodes have the same parent (the scene). Changing ignoresSiblingOrder to false will fix your problem but if you every want to set it to true you can order the scenes children and there children using zPosition. Just for future reference. – Nightly Jan 27 '15 at 18:16