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

Ios skView.ignoreSiblingOrder在swift中的重要性/效率?,ios,iphone,swift,Ios,Iphone,Swift,初始化场景时执行skView.ignoreSiblingOrder=true有多重要/效率 现在,我将其设置为true,但由于某种原因,当我从主菜单场景启动游戏场景时,它会在我的角色之前加载背景(即使背景代码先出现),但在我死后会修复,然后返回主菜单,然后加载另一个游戏场景。为了避免这个问题,我制作了一个布尔值,基本上可以检测我玩过多个游戏的时间。这是功能性的,但现在非常丑陋,我相信有一个更好的方法 代码: (接触开始) 游戏场景代码: override func didMoveToView(v

初始化场景时执行
skView.ignoreSiblingOrder=true
有多重要/效率

现在,我将其设置为true,但由于某种原因,当我从主菜单场景启动游戏场景时,它会在我的角色之前加载背景(即使背景代码先出现),但在我死后会修复,然后返回主菜单,然后加载另一个游戏场景。为了避免这个问题,我制作了一个布尔值,基本上可以检测我玩过多个游戏的时间。这是功能性的,但现在非常丑陋,我相信有一个更好的方法

代码: (接触开始)

游戏场景代码:

override func didMoveToView(view: SKView)
{
    TheGame = SKNode()
    self.addChild(TheGame)

    createSky()
    createGround()
    addFireButton()
    addJumpButton()
    addHero(view)       

}
重申一下,出于某种原因,我玩的第一个游戏背景在我的英雄和按钮之后呈现(如此之多),即使
createGround()
函数在之后运行。下面是一些函数

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)
    }
}
func addHero(视图:SKView){
//初始化我们的英雄并将其初始纹理设置为running1
hero=SKSpriteNode(纹理:heroAtlas.TextureName(“10Xmini_向导”))
hero.xScale=0.4
hero.yScale=0.4
hero.position=CGPointMake(frame.width/4.0,frame.height/4.0)
//为英雄创建一些CG值,以便在其物理定义中使用
让heroSize=CGSizeMake(hero.size.width,hero.size.height)
让heroCenter=CGPointMake(hero.position.x/2,hero.position.y/2)
self.addChild(英雄);
}
func createGround()
{
让groundTexture=SKTexture(图像名为:“bg”)
groundTexture.filteringMode=.Nearest
让moveGroundSprite=SKAction.moveByX(-groundTexture.size().width*2.0,y:0,duration:NSTimeInterval(0.01*groundTexture.size().width*1.0))
让resetGroundSprite=SKAction.moveByX(groundTexture.size().width*2.0,y:0,duration:0.0)
让MoveGroundSpriteSever=SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite,resetGroundSprite]))
对于变量i:CGFloat=0;i<2.0+self.frame.size.width/(groundTexture.size().width*2.0);+i
{
设sprite=SKSpriteNode(纹理:groundTexture)
sprite.physicsBody=SKPhysicsBody(矩形大小:CGSize(宽度:groundTexture.size().width,高度:frame.height/8))
sprite.physicsBody?.dynamic=false
sprite.physicsBody?恢复=0
sprite.setScale(2.0)
sprite.position=CGPointMake(i*sprite.size.width、sprite.size.height/2.0)
精灵运行动作(永远移动地面精灵)
游戏。添加孩子(精灵)
}
}

ignoresSiblingOrder
为false时,SpriteKit会按照节点在其父节点的
子节点中存在的顺序呈现节点-也就是说,数组顺序决定了哪个节点“在”另一个节点上绘制。这还意味着SpriteKit必须一次渲染一个节点,因此OpenGL绘图调用开销会降低效率


ignoresSiblingOrder
为true时,SpriteKit仅依赖
zPosition
属性来确定绘制顺序。这意味着它有时可以将同一个z方向上的所有内容合并到一个绘图中,从而加快渲染速度。但这也意味着,如果要控制哪些节点在哪些其他节点前面绘制,则需要适当地设置它们的
zPosition

那么你知道为什么我第一次运行场景时会出现小故障,但在一切正常之后都会出现吗?另外,你能进一步解释一下如何看到“zPosition”以便我的背景总是先呈现出来吗?至于第一个问题。。。你忽略了兄弟顺序,所以不能保证你会得到什么顺序。至于第二个。。。这说明了什么?取自:当此属性设置为“true”时,在确定渲染顺序时,将忽略节点在树中的位置。位于同一z位置的节点的渲染顺序是任意的,并且在每次渲染新帧时可能会更改。是否可以在子节点的顶部渲染父节点?假设我们将ignore sibling order设置为true,并且子节点的zPosition为0。我认为这段代码是用于运行播放器的,对吗?我想滑动那个玩家,你能告诉我怎么做吗?因为我是swift精灵套件的初学者
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)
    }
}