我正在尝试将onTapListener添加到节点,但是其中的代码从未被调用过。有人可以发现我在做的错误吗?

private fun addNodeToScene(fragment: ArFragment, createAnchor: Anchor, renderable: ModelRenderable) {
    val anchorNode = AnchorNode(createAnchor)
    anchorNode.setOnTapListener{_,_ ->
        buttonasd.text = "asdasd" // this line is never called
    }

    val transformableNode = TransformableNode(fragment.transformationSystem)
    transformableNode.renderable = renderable
    transformableNode.setParent(anchorNode)
    fragment.arSceneView.scene.addChild(anchorNode)
    transformableNode.select()
}

最佳答案

尝试一种稍微不同的方法,如下所示:

private var grid = Array(ROW_NUM) {
    arrayOfNulls<TranslatableNode>(COL_NUM)
}

val anchorNode = AnchorNode(hitResult.createAnchor())
anchorNode.setParent(arFragment.arSceneView.scene)

grid.matrixIndices { col, row ->
    val renderableModel = droidRenderable?.makeCopy() ?: return@matrixIndices
    TranslatableNode().apply {
        setParent(anchorNode)
        renderable = renderableModel
        addOffset(x = row * spacing, z = col * spacing)
        grid[col][row] = this
        this.setOnTapListener { _, _ ->
            // button.text = "asdasd"
        }
    }
}

希望这可以帮助。

10-05 19:50