ios - SKSpriteNode 隐藏在 UI 背景图像下

标签 ios swift uiimage skspritenode

我有一个可以正常运行的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/

相关文章:

ios - 编译项目时出现 Xcode 错误

ios - 带有 TextField 的 SwiftUI 列表在键盘出现/消失时进行调整

ios - 填写表格 View 部分和单元格

xcode - iOS 9 native 应用程序无法通过 https 连接到 mobilefirst 服务器

ios - 如何使用 Google Drive API v3 获取我的云端硬盘中的文件?

ios - 企业 Mac 应用分发

ios - Swift - 如何更新自定义 MKAnnotation 标注中的数据?

objective-c - 将 UIIImage 对象设置为 NSMutableDictionary 对我不起作用

ios - 在同一个 UIImageView 中加载多个图像会在显示新图像之前闪烁最后一个图像

ios - 获取 ImageView 图片原始大小