我想单击一个按钮以生成3D对象。我将SpritKit场景叠加到SceneKit场景上以在界面中使用SKNodes。但是由于某种原因未调用该函数,其余代码似乎仍在工作。我想知道原因,以及是否还有另一种调用此函数的方法
这是我要传递的功能。
func setupDice() {
var diceNode: SCNNode!
let size: CGFloat = 1.5
let diceGeometry = SCNBox(width: size, height: size, length: size, chamferRadius: 0)
diceGeometry.materials.removeFirst()
for i in 1...6 {
let diceMaterial = SCNMaterial()
diceMaterial.locksAmbientWithDiffuse = true
diceMaterial.diffuse.contents = UIImage(named: "x\(i)")?.xFlipped
diceMaterial.blendMode = .multiply
diceGeometry.materials.append(diceMaterial)
}
diceNode = SCNNode(geometry: diceGeometry)
diceNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
diceNode.physicsBody?.restitution = 1
diceNode.physicsBody?.mass = 1
diceNode.physicsBody?.categoryBitMask = 1
diceNode.physicsBody?.collisionBitMask = 2
diceNode.physicsBody?.applyTorque(SCNVector4(x: randomTorqueForce(),
y: randomTorqueForce(),
z: randomTorqueForce(),
w: randomTorqueForce()), asImpulse: true)
self.rootNode.addChildNode(diceNode)
}
这是我在SpriteKit场景内调用函数的地方
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if touch == touches.first {
if playButtom.contains(touch.location(in: self)){
mainScene.setupDice()
}
}
}
}
这是我的GameViewController的正确状态
class GameViewController: UIViewController {
var sceneView: SCNView!
var spriteScene: OverlayScene!
override func viewDidLoad() {
super.viewDidLoad()
//setupTest()
self.sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
self.sceneView.scene = MainScene()
spriteScene = OverlayScene(size: view.bounds.size)
sceneView.backgroundColor = UIColor.white
sceneView.overlaySKScene = spriteScene
sceneView.allowsCameraControl = true
sceneView.isPlaying = true
self.view.addSubview(self.sceneView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
可能是因为您在mainScene
的初始化阶段未设置OverlayScene
属性。这样的事情应该起作用:
class GameViewController: UIViewController {
var sceneView: SCNView!
var spriteScene: OverlayScene!
override func viewDidLoad() {
super.viewDidLoad()
//setupTest()
self.sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let mainScene = MainScene()
self.sceneView.scene = mainScene
let overlayScene = OverlayScene(size: view.bounds.size)
overlayScene.mainScene = mainScene
spriteScene = overlayScene
sceneView.backgroundColor = UIColor.white
sceneView.overlaySKScene = spriteScene
sceneView.allowsCameraControl = true
sceneView.isPlaying = true
self.view.addSubview(self.sceneView)
}