如何根据请求在SceneKit中添加动画

时间:2015-11-03 06:20:00

标签: swift animation scenekit

如果您可以帮助我将此动画添加到场景中,或者如果您知道如何将动画添加到场景中,我们将非常感激。

代码:

let BallScene = SCNScene(named: "art.scnassets/Ball.dae")!
let ManScene = SCNScene(named: "art.scnassets/Genric_Simplifiyed9.dae")!
let Man: SCNNode = ManScene.rootNode.childNodeWithName("Bob_001", recursively: true)!
Man.position = SCNVector3(x: 0, y: 0, z: 5)
BallScene.rootNode.addChildNode(Man)

let Animation: CAAnimation
let AnimationUrl: NSURL = NSBundle.mainBundle().URLForResource("art.scnassets/Genric_Simplifiyed9.dae", withExtension: "dae")!
let sceneSource = SCNSceneSource(URL: AnimationUrl, options: [SCNSceneSourceAnimationImportPolicyKey : SCNSceneSourceAnimationImportPolicyDoNotPlay])

sceneSource.addAnimation(attackAnimation, forKey: "attack”)

1 个答案:

答案 0 :(得分:1)

如果您有带动画的DAE / COLLADA文件,只需单击“播放”按钮即可在故事板中播放动画。 那么你可以

let subScene = SCNScene(named: "art.scnassets/test_ball_2011.dae")!
let childNodes = subScene.rootNode.childNodes
for child in childNodes {
    //child.position = SCNVector3(0, 0, -100)
    rootScene.rootNode.addChildNode(child)
}

动画将在你的rootScene中播放

你可以使用

for child in childNodes {
   let keys = child.animationKeys
   child.position = SCNVector3(0, 0, -100)
   for key in keys {
       let animation = child.animationForKey(key)!
       animation.repeatCount = 5
       child.removeAnimationForKey(key) 
       child.addAnimation(animation, forKey: key)
   }
   rootScene.rootNode.addChildNode(child)

设置位置或repeatCount ......

您的代码的另一种方式: 你可以用

let url = NSBundle.mainBundle().URLForResource(/* your DAE file */)
let sceneSource = SCNSceneSource(URL: url, options: [
        SCNSceneSourceAnimationImportPolicyKey : SCNSceneSourceAnimationImportPolicyPlayRepeatedly
])!

获取源代码,在选项中可以设置很多东西,只需检查API即可 并使用

let node = sceneSource.entryWithIdentifier("/* the node name */", withClass: SCNNode.self)
let animation = sceneSource.entryWithIdentifier("/* the animation name */", withClass: CAAnimation.self)

获取节点和动画。如果这两个文件在不同的文件中,只需要获取2个来源:) 最后一件事是node.addAnimation(animation, forKey: nil) //检查API