gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:44:56 24 4
gpt4 key购买 nike

我想点击一个按钮来生成一个 3D 对象。我将 SpritKit 场景叠加到 SceneKit 场景上,以便在我的界面中使用 SKNode。但由于某种原因,该函数没有被调用,其余代码似乎可以正常工作。我想知道这是什么原因,如果有另一种调用这个函数的方法

这是我要传递的函数。

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

最佳答案

可能是因为您在 OverlayScene 的初始化阶段没有设置 mainScene 属性。这样的事情应该有效:

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

关于ios - 我正在尝试将命令从 SKScene(SpriteKit) 传递到 SCNScene(SceneKit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51371245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com