Game Development Reference
In-Depth Information
NSLog(@"completed animation sequence: %@", name);
}
Every time this method runs, it will log the name of the animation whose playback ended.
The method also runs whenever a looping animation has reached the end of its animation
cycle and starts over. This allows you to wait for a looping animation to play back to com-
plete before stopping or replacing it with another animation, in order to prevent nasty
jumps in the animation playback.
Alternatively, you can also set a block that runs whenever an animation ends. The advant-
age is that you do not have to implement the
CCBAnimationManagerDelegate
pro-
tocol in the current class to do so; the disadvantage is that you get the
CCAnima-
tionManager
as input, so you have to refer to the
lastCompletedSequenceName
property to get the name of the Timeline that ended. It's also slightly more verbose to set
an animation callback block as you can see in
Listing 5-5
.
Listing 5-5
.
Using an animation callback block to be notified of “animation ended”
events
[sawNoAutoplay.animationManager
setCompletedAnimationCallbackBlock:^(CCAnimationManager*
sender) {
NSLog(@"completed animation sequence: %@",
sender.lastCompletedSequenceName);
}];
Deciding whether to implement the
CCBAnimationManagerDelegate
protocol or
use the callback block variant is a matter of personal preference.
Differentiating Between Animations
Note once again that each CCB file has its own
CCAnimationManager
instance. In
this instance, only the animations in
Saw1_noautoplay.ccb
will call the callback
method in
Listing 5-4
.
Likewise, if you were to use
self.animationManager.delegate = self;
, the
method in
Listing 5-4
would run every time a Timeline animation of the
GameS-
cene.ccb
ends. However, it would not run, for example, when a Timeline animation of
Player.ccb
or
Background1.ccb
ends.
Search WWH ::
Custom Search