隐藏在UI背景图像下的SKSpriteNode

时间:2016-01-16 21:04:53

标签: 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))
} 

}

1 个答案:

答案 0 :(得分:2)

对于绘制顺序,请使用zPosition属性:

  

在您的情况下,您需要将要显示的精灵设置为比后面显示的值更高的.zPosition值。

示例:ball.zPosition = 10

zPosition 定义

节点相对于其父节点的高度。

<强> TIPPS: 默认值为0.0。正z轴向观察者投射,使得具有较大z值的节点更靠近观察者。