Swift ARKit-SKScene内的SKLabelNode不使用SKAction更新

时间:2018-10-22 16:56:39

标签: swift arkit skaction skscene arscnview

我在SKScene内有一个SKLabelNode,当识别3D对象时会弹出该标签,此刻我正试图使其贴标签,以便标签每秒更新一次。但是,目前无法使用。所以我的问题是,我该如何进行这项工作?非常感谢您的帮助,非常感谢!

ARSCNView:

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

    let node = SCNNode()

    if let objectAnchor = anchor as? ARObjectAnchor {
        let plane = SCNPlane(width: CGFloat(objectAnchor.referenceObject.extent.x * 0.8), height: CGFloat(objectAnchor.referenceObject.extent.y * 0.5))
        plane.cornerRadius = plane.width / 8

        guard let spriteKitScene = SKScene(fileNamed: "ProductInfo") as? ProductInfo else { return node }

        plane.firstMaterial?.diffuse.contents = spriteKitScene
        plane.firstMaterial?.isDoubleSided = true
        plane.firstMaterial?.diffuse.contentsTransform = SCNMatrix4Translate(SCNMatrix4MakeScale(1, -1, 1), 0, 1, 0)

        let planeNode = SCNNode(geometry: plane)
        planeNode.position = SCNVector3Make(objectAnchor.referenceObject.center.x, objectAnchor.referenceObject.center.y + 0.35, objectAnchor.referenceObject.center.z)
        node.addChildNode(planeNode)
    }
    return node
}

SKScene

class ProductInfo: SKScene {

    var value = Int()

    override func sceneDidLoad() {
        let wait = SKAction.wait(forDuration: 1.0)
        let update = SKAction.run { self.updateValue() }
        let seq = SKAction.sequence([wait, update])
        let rep = SKAction.repeatForever(seq)
        run(rep)
    }

    func updateValue() {
        value = value + 1
        if let label = self.childNode(withName: "myLabel") as? SKLabelNode {
            label.text = String(value)
        }
    }
}

0 个答案:

没有答案