简体   繁体   中英

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. I overlayed a SpritKit Scene onto a SceneKit Scene to use SKNodes in my interface. 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

    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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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