我有一个可以正常运行的SKSpriteNode
。但是,当我添加 UIImage
时,SKSpriteNode
会隐藏在 UIImage
后面。我一直试图找出原因,但我遇到了一点麻烦,似乎无法弄清楚我缺少什么来允许 SKSSpriteNode 出现在 UI 背景图像的顶部,而不是在它后面看不见的地方。任何帮助将不胜感激!
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
import SpriteKit
import SceneKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var blueBall:SKSpriteNode!
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
self.physicsWorld.contactDelegate = self
blueBall = SKSpriteNode( imageNamed: "ball")
blueBall.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
blueBall.physicsBody = SKPhysicsBody(circleOfRadius: blueBall.size.width / 1.5 )
blueBall.physicsBody!.dynamic = true
blueBall.physicsBody!.allowsRotation = false
self.addChild(blueBall)
}
override func touchesBegan(touches: Set<UITouch> , withEvent event: UIEvent?) {
self.blueBall.physicsBody?.velocity = CGVectorMake(35, 0)
self.blueBall.physicsBody?.applyImpulse(CGVectorMake(4, 10))
}
}
最佳答案
对于绘制顺序,请使用 zPosition
属性:
In your case you will need to give the Sprite you want to be displayed in front a higher
.zPosition
value than the one to be displayed further back.
示例:ball.zPosition = 10
zPosition
定义:
节点相对于其父节点的高度。
提示: 默认值为 0.0。正 z 轴投影向观察者,以便 z 值较大的节点更靠近观察者。
关于ios - SKSpriteNode 隐藏在 UI 背景图像下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34832126/