SKAction对复制子项的怪异行为

问题描述 投票:0回答:1

嗯,我的目标是提取SKScene的内容,然后对它们的节点运行一些操作:

func loadSceneNodes() {
    let loadedScene = SKScene(fileNamed: "newScene.sks")

    // Reads the content of the "Content" node in the newScene.sks
    let content = loadedScene!.childNode(withName: "Content") as! SKSpritenode

    // I copy the content in order to avoid "node already has a parent" error
    let copiedContent = content.copy() as! SKSpriteNode

    addChild(copiedContent)

    // Try to run a test action on the nodes extracted
    copiedContent.run(.rotate(byAngle: 90, duration: 2.0)) {
        print("DONE") // DEBUG
    }
}

发生的事情是,我成功阅读了内容并将其显示在场景中,但是未触发动作。该节点将不会旋转,并且print("DONE")甚至都不会得到呼叫。

我想念什么?

---编辑---

我注意到,如果我手动复制元素:

func loadSceneNodes() {
    let loadedScene = SKScene(fileNamed: "newScene.sks")

    // Reads the content of the "Content" node in the newScene.sks
    let content = loadedScene!.childNode(withName: "Content") as! SKSpritenode

    // I MANUALLY copy the content
    let copiedContent = SKSpriteNode(color: content.color, size: content.size)
    copiedContent = content.position

    addChild(copiedContent)

    // Try to run a test action
    copiedContent.run(.rotate(byAngle: 90, duration: 2.0)) {
        print("DONE") // IT WORKS
    }
}

一切正常。错误或我缺少什么?

swift xcode sprite-kit game-development skaction
1个回答
0
投票

在sknode上使用moveToParent而不是在父节点上使用addChild,以避免创建副本。

© www.soinside.com 2019 - 2024. All rights reserved.