简体   繁体   English

我正在尝试将命令从SKScene(SpriteKit)传递到SCNScene(SceneKit)

[英]I'm trying to pass a command from a SKScene(SpriteKit) to an SCNScene(SceneKit)

I want to click a button to spawn a 3D object. 我想单击一个按钮以生成3D对象。 I overlayed a SpritKit Scene onto a SceneKit Scene to use SKNodes in my interface. 我将SpritKit场景叠加到SceneKit场景上以在界面中使用SKNodes。 But for some reason the function is not being called, the rest of the code seems to be working. 但是由于某种原因未调用该函数,其余代码似乎仍在工作。 I would like to know the reason for this, and if there is another way of calling this function 我想知道原因,以及是否还有另一种调用此函数的方法

This is the function I'm trying to pass. 这是我要传递的功能。

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)


}

This is where I'm calling the function inside of my SpriteKit Scene 这是我在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()
            }
        }
    }


}

and this is the correct state of my GameViewController 这是我的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.
}

} }

Probably because you didn't set the mainScene property during the initialization phase of your OverlayScene . 可能是因为你没有设置mainScene在你的初始化阶段属性OverlayScene Something like this should works: 这样的事情应该工作:

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM