Cocos2d, swift find when Spritebuilder timeline animation completed via callback

115 Views Asked by At

I have a callback setup on timeline in Spritebuilder. and I have the timeline animation triggered. My goal is to disable touch until animation completes.

func triggerTimelineAction(nodeName: CCNode){
    self.userInteractionEnabled = false;
nodeName.animationManager.runAnimationsForSequenceNamed("nodeMainAction")
nodeName.animationManager.setCompletedAnimationCallbackBlock(b: ??? )){
self.userInteractionEnabled = true;
}
}

Question: how do I declare callback keyframe and hookup/receive it.

UPD: i was told to pass a function in setCompletedAnim... so i made my code look like: func animationCompleted(){ self.userInteractionEnabled = true; }

func triggerTimelineAction(nodeName: CCNode){
self.userInteractionEnabled = false;
       nodeName.animationManager.runAnimationsForSequenceNamed("nodeMainAction")
nodeName.animationManager.setCompletedAnimationCallbackBlock(b: @selector(animationCompleted))

}

on which I received error "Cannot convert value of type ()->() to expected argument type (AnyObject)->Void

Apparently this is more related to Cocos2d syntaxis. will dig further in that direction

1

There are 1 best solutions below

0
user3424586 On

So after a session with codementor I figured out that I was using the setCompletedAnimationCallbackBlock function improperly.

In this code
activeNode.animationManager.setCompletedAnimationCallbackBlock(b: ((AnyObject!) -> Void) 

I was trying to pass arguments as AnyObject when in fact this is a block that this function returns. So this code has worked for me

activeNode.animationManager.setCompletedAnimationCallbackBlock { (object: AnyObject!) -> Void in
  print(object)
}

where object is just firs time declared name for element that gets returned. The print function showed that object that returns is a CCAnimationManager so it is equal to previous syntax when you had to runAction then assign action.delegate to self and then actually catch callback. Hope this information helps somebody.