初始化场景时skView.ignoreSiblingOrder = true
有多重要/有效?
现在,我将它设置为true,但出于某种原因,当我从MainMenu场景开始我的GameScene时,它会在我的角色之前加载背景(即使背景的代码是第一个),但它修复了一旦我死了,回到主菜单,然后加载另一个游戏场景。为了避免这个问题,我制作了一个布尔值,它基本上可以检测我何时玩过多个游戏。它现在很实用,但非常难看,我确信有更好的方法。
代码: (在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)
}
重申一下,出于某种原因,即使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)
}
}