skift.ignoreSiblingOrder在swift中的重要性/效率?

初始化场景时, skView.ignoreSiblingOrder = true有多重要/有效?

现在,我把它设置为true,但是由于某种原因,当我从MainMenu场景开始我的GameScene时,它会在我的angular色之前加载背景(尽pipe背景的代码是第一个),但是一旦我死了就返回到主菜单,然后加载另一个游戏场景。 为了避免这个问题,我做了一个布尔值,基本上检测了我玩过多个游戏的时间。 它现在是function,但非常丑陋,我相信有一个更好的方法。

代码:(在touchesBegan)

  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) } 

重申一下,出于某种原因,我玩后台的第一个游戏会渲染我的英雄和button,即使createGround()函数在后面运行。 以下是这些function。

 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) } } 

ignoresSiblingOrder为false时,SpriteKit按照它们存在于其父数组的children数组中的顺序呈现节点 – 也就是说,数组顺序决定了哪一个节点在另一个节点上绘制“顶部”。 这也意味着SpriteKit必须每次渲染一个节点,所以OpenGL的绘制调用开销会降低效率。

ignoresSiblingOrder为true时,SpriteKit完全依赖zPosition属性来确定要绘制的顺序。这意味着它有时可以将同一个z中的所有东西合并为一个绘制,这使得渲染速度更快。 但是这也意味着如果你想控制哪些节点在其他节点前面绘制,则需要适当地设置它们的zPosition