删除节点会导致保留周期

时间:2017-03-06 03:18:05

标签: swift scenekit

在我的场景包游戏中,节点随机添加到场景中。这些节点沿一个方向行进,直到它们超过所需的最终位置,然后它们将被移除。他们可以向上,向下,向左和向右行进。 (我使用node.runAction(SCNAction.repeatActionForever(SCNAction.moveBy(...

以下是我用来检测节点何时超过其结束位置以便删除它们的内容。

我遇到的问题是,虽然这有效,但由于某种原因,它导致了 SCNActionMove SCNActionRepeat 的保留周期。

我发现避免这种情况的唯一方法是在游戏结束后在for循环中一次删除所有节点,但这并不理想,因为游戏可以玩很长时间。

谢谢!

func renderer(renderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {

    // If any nodes have been spawned
    if spawnedNodeArray.count > 0 {

        // Get the first spawned node
        let node = rootNode.childNodeWithName(spawnedNodeArray[0].nodeName, recursively: true)!

        // If node is moving RIGHT, check if node has reached end position
        if spawnedNodeArray[0].Direction == "RIGHT" {
            // If node has reached past end position...
            if node.position.x >= Float(spawnedNodeArray[0].EndXPos) {
                node.removeAllActions()
                node.removeFromParentNode()
                spawnedNodeArray.removeAtIndex(0)
            }
        }

        // If node is moving LEFT, check if node has reached end position
        else if spawnedNodeArray[0].Direction == "LEFT" {
            // If node has reached past end position...
            if node.position.x <= Float(spawnedNodeArray[0].EndXPos) {
                node.removeAllActions()
                node.removeFromParentNode()
                spawnedNodeArray.removeAtIndex(0)
            }
        }

        // If node is moving DOWN, check if node has reached end position
        else if spawnedNodeArray[0].Direction == "DOWN" {
            // If node has reached past end position...
            if node.position.z >= Float(spawnedNodeArray[0].EndZPos) {
                node.removeAllActions()
                node.removeFromParentNode()
                spawnedNodeArray.removeAtIndex(0)
            }
        }

        // If node is moving UP, check if node has reached end position
        else if spawnedNodeArray[0].Direction == "UP" {
            // If node has reached past end position...
            if node.position.z <= Float(spawnedNodeArray[0].EndZPos) {
                node.removeAllActions()
                node.removeFromParentNode()
                spawnedNodeArray.removeAtIndex(0)
            }
        }
    }
}

SCNActionRepeat SCNActionMove

2 个答案:

答案 0 :(得分:1)

不是你问的问题的直接答案,而是......

如何保留可重用节点池?不是在节点死亡时移除节点,而是将其hidden属性设置为true并将其添加到重用队列。首先尝试使现有节点出列,而不是始终创建新节点。

答案 1 :(得分:0)

我已经想出如何停止保留周期。

我必须给动作一个键,然后不要删除所有动作,而只是删除键的动作。

示例:

node.runAction(..., forKey: "moveAction") 

node.removeActionForKey("moveAction")