Hunter Allen Hunter Allen -5 years ago 283
Swift Question

"Cannot convert value" error on enumerateChildNodesUsingBlock call

Im getting an Error on the 4th line of code I attached a picture to this question that should explain everything its probably a quick fix but I've never seen it.

enter image description here


let GuyScene = SCNScene(named: "art.scnassets/TestMan1.scn")
let Guy: SCNNode = GuyScene!.rootNode.childNodeWithName("Man", recursively: true)!
let collisionCapsuleRadius2 = CGFloat(0.1)
let collisionCapsuleHeight2 = CGFloat(0.1)
Guy.position = SCNVector3(x: -30.0, y: 30.0, z: 0.0)
Guy.scale = SCNVector3Make(5, 5, 5)
Guy.rotation = SCNVector4Make(0, 1, 0, 1 )
//----Giveing it a physics---------
Guy.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius2, height: collisionCapsuleHeight2), options:nil))
Guy.physicsBody?.affectedByGravity = true
Guy.physicsBody?.friction = 0 //
Guy.physicsBody?.restitution = 1 //bounceness of the object
Guy.physicsBody?.angularDamping = 1 // rotationess
Guy.physicsBody?.mass = 1
Guy.physicsBody?.rollingFriction = 0


func loadAnimationFromSceneNamed(path: String) -> CAAnimation {
var scene: SCNScene = SCNScene(named: path )!
var animation: CAAnimation? = nil
scene.rootNode.enumerateChildNodesUsingBlock({(child: SCNNode, stop: Bool) -> Void in
if child.animationKeys.count > 0 {
animation = child(animationForKey: child.animationKeys[0])
stop = true
return animation!

var scenePath: String = "art.scnassets/Animation1.scn"

var thisAnimation: CAAnimation = loadAnimationFromSceneNamed(scenePath)

var timer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: "Start", userInfo: nil, repeats: true)

func Start() {
Guy.addAnimation(thisAnimation, forKey: "thisAnimationKey")

Below Is A picture of the errors I get when I do the suggested answers to this question

enter image description here

Extra Argument ??? Thanks almost there lol

enter image description here

Answer Source

The simplest way to fix this problem is just to drop the explicit type annotations and let Swift infer them for you:

scene.rootNode.enumerateChildNodesUsingBlock({child, stop in

    // ...

    // to stop the enumeration (as stop is an UnsafeMutablePointer to an ObjCBool)
    stop.memory = true

You can also use trailing closure syntax to make the call look slightly neater:

scene.rootNode.enumerateChildNodesUsingBlock {child, stop in
    // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download