我在使用用于在sprite Kit中建立zPositiion的枚举时遇到了一些问题。这是枚举:
enum Layer:CGFloat {
case Sky = 0
case Clouds = 1
case Background = 2
case Foreground = 3
case Character = 4
case Foreground1 = 5
}
然后我将我的场景分成不同的图层以包含额外的效果。我将worldNode添加为spriteKit基本节点,然后他们的子节点如下:
let backGroundMountain = SKSpriteNode(imageNamed: "Scene3BackgroundMountain1")
backGroundMountain.zPosition = Layer.Background.rawValue
worldNode.addChild(backGroundMountain)
let backGroundMountainOver = SKSpriteNode(imageNamed: "Scene3BackgroundMountainOver1")
backGroundMountainOver.zPosition = Layer.Foreground.rawValue
worldNode.addChild(backGroundMountainOver)
let greenCircle = SKSpriteNode(imageNamed: "green_circle_1")
greenCircle.zPosition = Layer.Character.rawValue
backGroundMountainOver.addChild(greenCircle)
let foreGroundMountainRight = SKSpriteNode(imageNamed: "Scene3FrontMountain1")
foreGroundMountainRight.zPosition = Layer.Foreground1.rawValue
worldNode.addChild(foreGroundMountainRight)
如您所见,greenCircle是backGroundMountainOver的子节点,而foreGroundMountainRight是worldNode的子节点。我对前面提到的效果要求这个。
问题是虽然foreGroundMountainRight的zPosition比greenCircle的zPosition更大,但最后一个出现在第一个上面。解决问题的唯一方法是改变:
… case Foreground1 = 7
我不明白这是怎么回事。
答案 0 :(得分:1)
这是因为你在已经在zIndex = 3(backGroundMountainOver)的节点上添加了greenCircle节点(zIndex 4),总共给出了zIndex 7.当foreGroundMountainRight附加到world节点时(我假设默认为zIndex) = 0)在zIndex = 5上。