Importance / Effectiveness of skView.ignoreSiblingOrder in Swift?
How important / efficient is it to execute skView.ignoreSiblingOrder = true
when initializing a scene?
I have now set to true, but for some reason, when I run my GameScene from my MainMenu scene, it loads a background file in front of my character (even if the backgound code comes first), but it fixes when I die and go back to the main menu and then load another game scene. To avoid this problem, I made a boolean that basically determines when I've played more than one game. It works right now but is very ugly and I'm sure there is a better way.
code: (In contactsBegan)
let skView = self.view as SKView!
skView.showsFPS = true
skView.showsNodeCount = true
if spriteNode.name == "StartButton"
{
/* Sprite Kit applies additional optimizations to improve rendering performance */
//sets ignoreSiblingOrder to false the first game because of XCode Glitch where background was rendering over player for some reason
skView.ignoresSiblingOrder = false
if(Game){
skView.ignoresSiblingOrder = true
}
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene
{
skView.presentScene(scene)
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
}
GameSceneCode:
override func didMoveToView(view: SKView)
{
TheGame = SKNode()
self.addChild(TheGame)
createSky()
createGround()
addFireButton()
addJumpButton()
addHero(view)
}
To recap, for some reason the first game I play the background returns after (so over) my hero and buttons, even if the function createGround()
works after. Following are the following functions.
func addHero(view: SKView){
//initializes our hero and sets his initial texture to running1
hero = SKSpriteNode(texture: heroAtlas.textureNamed("10Xmini_wizard"))
hero.xScale = 0.4
hero.yScale = 0.4
hero.position = CGPointMake(frame.width / 4.0, frame.height / 4.0)
//creates some CG values for the hero to be used in its physics definitions
let heroSize = CGSizeMake(hero.size.width, hero.size.height)
let heroCenter = CGPointMake(hero.position.x/2, hero.position.y/2)
self.addChild(hero);
}
func createGround()
{
let groundTexture = SKTexture(imageNamed: "bg")
groundTexture.filteringMode = .Nearest
let moveGroundSprite = SKAction.moveByX(-groundTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.01 * groundTexture.size().width * 1.0))
let resetGroundSprite = SKAction.moveByX(groundTexture.size().width * 2.0, y: 0, duration: 0.0)
let moveGroundSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite, resetGroundSprite]))
for var i:CGFloat = 0; i < 2.0 + self.frame.size.width / (groundTexture.size().width * 2.0); ++i
{
let sprite = SKSpriteNode(texture: groundTexture)
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: groundTexture.size().width, height: frame.height/8))
sprite.physicsBody?.dynamic = false
sprite.physicsBody?.restitution = 0
sprite.setScale(2.0)
sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2.0)
sprite.runAction(moveGroundSpritesForever)
TheGame.addChild(sprite)
}
}
source to share
When ignoresSiblingOrder
false, SpriteKit displays nodes in the order in which they exist in the parent array children
, that is, the order of the array determines which one draws "on top" of the other. This also means that SpriteKit has to render each node one at a time, so you lose efficiency to retrieve the OpenGL call.
When ignoresSiblingOrder
true, SpriteKit relies solely on the property zPosition
to determine which order to draw. This means that it can sometimes combine everything at the same z into one draw, which makes rendering faster. But this also means that if you want to control which nodes are drawn in front of others, you need to set them zPosition
accordingly.
source to share