节点未在Swift 2和SpriteKit

时间:2015-09-17 14:35:03

标签: ios swift sprite-kit swift2 sktexture

所以我正在制作一个沙子下降益智游戏,你必须让沙粒进入目标。在Swift 2,iOS 9,xCode 7更新之前,我的节点工作正常。但现在,当我加载游戏时,它并没有正确加载所有节点。

以此为例:(我评论了大部分细节,我知道不是问题,例如标签的定位和颜色)

override func didMoveToView(view: SKView) {

    self.physicsWorld.gravity = CGVectorMake(0.0, -4.8) // Rate of Gravity is set here

    // Background
    bg = SKSpriteNode(texture: SKTexture(imageNamed: "settingBG.png"))
    // position and size declared here
    self.addChild(bg)

    // Back Button
    back = SKSpriteNode(texture: SKTexture(imageNamed: "back.png"))
    // position and size declared here
    back.name = "back"

    self.addChild(back)

    // Tutorial Toggle
    tutorial = SKSpriteNode(texture: SKTexture(imageNamed: "tutorial.png"))
    // position and size declared here
    tutorial.name = "tutorial"

    self.addChild(tutorial)

    // High Scores
    redLabel = SKLabelNode(fontNamed: "QuicksandBold-Regular")
    // font size and color here

    blueLabel = SKLabelNode(fontNamed: "QuicksandBold-Regular")
    // font size and color here

    yellowLabel = SKLabelNode(fontNamed: "QuicksandBold-Regular")
    // font size and color here

    // label positions here

    redPoints = SKLabelNode(fontNamed: "QuicksandBold-Regular")
    // font size and color here

    bluePoints = SKLabelNode(fontNamed: "QuicksandBold-Regular")
    // font size and color here

    yellowPoints = SKLabelNode(fontNamed: "QuicksandBold-Regular")
    // font size and color here

    // set up variables in labels from UserDefaults

    // points position here

    self.addChild(redLabel)
    self.addChild(redPoints)
    self.addChild(blueLabel)
    self.addChild(bluePoints)
    self.addChild(yellowLabel)
    self.addChild(yellowPoints)

    // Level Selector
    chooseLabel = SKLabelNode(fontNamed: "QuicksandBold-Regular")
    // size and color

    chooser = SKSpriteNode(texture: SKTexture(imageNamed: "levelSelector.png"), size: CGSize(width: 75, height: 75))

    number = SKLabelNode(fontNamed: "QuicksandBold-Regular")
    // size and color

    leftArrow = SKSpriteNode(texture: SKTexture(imageNamed: "leftarrow.png"), size: CGSize(width: 70, height: 50))
    rightArrow = SKSpriteNode(texture: SKTexture(imageNamed: "rightarrow.png"), size: CGSize(width: 70, height: 50))

    highscore = SKLabelNode(text: "QuicksandBold-Regular")
    // size and color

    // text for labels

    // positions for everything else

    self.addChild(chooseLabel)
    self.addChild(chooser)
    self.addChild(number)
    self.addChild(leftArrow)
    self.addChild(rightArrow)
    self.addChild(highscore)


}

这应该在加载时将所有节点渲染到屏幕上,但是,许多节点都丢失了,并且在许多情况下,每次运行时缺少的节点都不同。

run one

run two

有什么见解?

1 个答案:

答案 0 :(得分:3)

尝试将ZPosition设置为您的节点,对我有用。

在我的项目中我遇到这种情况:

let bg = SKSpriteNode(imageNamed: "careSceneBG2")
bg.zPosition = 0
...

let monster = SKSpriteNode(imageNamed: "fire")
monster.zPosition = 1
...

let item = SKSpriteNode(imageNamed: img)
item.zPosition = 2
...