gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:15:50 24 4
gpt4 key购买 nike

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

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

代码:(在touches开始)

                    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

}

游戏场景代码:

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

最佳答案

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

ignoresSiblingOrder 为真时,SpriteKit 完全依赖于 zPosition 属性来确定绘制的顺序。这意味着它有时可以将同一 z 处的所有内容组合成一次绘制,这使得渲染速度更快。但这也意味着如果你想控制哪些节点绘制在其他节点之前,你需要适本地设置它们的 zPosition

关于ios - skView.ignoreSiblingOrder 在 swift 中的重要性/效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com