简体   繁体   English

添加 SceneKit 约束时应用程序崩溃

[英]App crashes when adding SceneKit constraints

I tried to add the constraints for label and add it into the view.我尝试为标签添加约束并将其添加到视图中。

View is added into the node getting crash.视图被添加到节点崩溃。

func createNode() {
    let node = SCNNode(geometry: SCNPlane(width: 0.5, height: 0.8)) 
    let view = UIView(frame: CGRect (x: 0, y: 0, width: 200, height: 200))
    view.backgroundColor = UIColor.green
    let label = UILabel()
    label.text = "Hello"
    view.addSubview(label)

    label.topAnchor.constraint(equalTo: view.topAnchor, 
                              constant: 18).isActive = true
    label.leadingAnchor.constraint(equalTo: view.leadingAnchor, 
                                 constant: 20).isActive = true
    label.widthAnchor.constraint(equalToConstant:100).isActive = true
    label.heightAnchor.constroint(equalToConstant: 100).isActive = true

    node.geometry.firstMaterial.diffuse.contents = view
    node.position = SCNVector3(0, 0,-1)  
    scienview.scene.rootNode.addchildNode(node)
}

Anyone help me.任何人都可以帮助我。 崩溃的日志 . . Added inside of main thread also but not working.在主线程内部也添加了但不起作用。

This post (see answer) for basic game layout example: 58103566基本游戏布局示例的这篇文章(见答案):58103566

As mentioned by Larme and you, set your alignment and constraints in viewDidLoad() of GameViewController: UIViewController正如 Larme 和你所提到的,在 GameViewController 的 viewDidLoad() 中设置对齐和约束: UIViewController

For most games, there is some kind of state that the game is in and I have a separate class for it.对于大多数游戏,游戏处于某种状态,我有一个单独的类。
GameNodes {}游戏节点{}

  • InitializeLevel - create nodes, etc. If you start timers, make sure they are in the main thread. InitializeLevel - 创建节点等。如果您启动计时器,请确保它们在主线程中。
  • Start - game logic开始 - 游戏逻辑
  • End - game logic结束 - 游戏逻辑
  • Cleanup - remove nodes and graphics清理 - 删除节点和图形

I use delegates to communicate between VC's.我使用委托在 VC 之间进行通信。 If the UI needs to be updated, then something like: GameViewController如果 UI 需要更新,则类似于:GameViewController

func showGameStatus(vText: String)
    {
        DispatchQueue.main.async {
            if(vText != "")
            {
                self.lblGameStatus.text = vText
                self.lblGameStatus.isHidden = false
                
            }
            else
            {
                self.lblGameStatus.isHidden = true
            }
        }
    }

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

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