【发布时间】:2015-02-26 02:38:48
【问题描述】:
与大多数游戏一样,我的游戏有一个场景和 HUD (SKScene)。为了避免与 HUD 元素重叠,我需要计算我的主要对象的投影大小。因为每个 iPhone 型号的对象的 HUD 尺寸和投影大小都不同,所以我需要一种动态执行此操作的方法,为此我需要获取我的主要对象的投影大小。
对我来说显而易见的方法是使用 xFov 和 yFov,但由于某种原因它们总是为零。 usesOrthographicProjection 设置为 false 所以不是因为这个。
下面是一个简单的测试代码,它试图将场景的平面与 HUD 上的平面相等。
import UIKit
import QuartzCore
import SceneKit
import SpriteKit
class GameViewController: UIViewController {
var hud:SKScene!
override func viewDidLoad() {
super.viewDidLoad()
// create a new scene
let scene = SCNScene()
// create and add a camera to the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
// retrieve the SCNView
let scnView = self.view as SCNView
// set the scene to the view
scnView.scene = scene
let sCPlaneGeo = SCNPlane(width: 5, height: 5)
let sCPlane = SCNNode(geometry: sCPlaneGeo)
scene.rootNode.addChildNode(sCPlane)
hud = SKScene(size: view.bounds.size)
scnView.overlaySKScene = hud
let sKplane = SKShapeNode(rectOfSize: CGSizeMake(50, 50))
sKplane.lineWidth = 1
sKplane.position = CGPointMake(view.bounds.size.width/2, view.bounds.size.height/2)
sKplane.strokeColor = UIColor.redColor()
hud.addChild(sKplane)
println(cameraNode.camera?.xFov) //Prints Zero
}
}
【问题讨论】:
-
如果不改变xFov或yFov,发现Fov是60度。不过,不知道如何从点转换为像素。也想知道为什么我得到了否定。
标签: swift sprite-kit scenekit