views:

540

answers:

1

I am currently using the cocos2d Director for controlling my animation with the pause, resume, and stopAnimation methods. Is it also possible to use the Director to return the time that the animation has played?

I am currently using this method:

-(void)stopAnimation:(id)sender {
    //Timer initialized elsewhere: startTimer = [NSDate timeIntervalSinceReferenceDate];
    //Do other method stuff here 

    [[Director sharedDirector] stopAnimation];
    stopTimer = [NSDate timeIntervalSinceReferenceDate];
    elapsedTime = (stopTimer - startTimer);
    NSLog(@"elapsedTime = %f", elapsedTime);
}
A: 

I looked through the Director source and didn't see anything that would help you. I did notice that your code, as written, doesn't take into account the time your animation was paused or when other scenes were playing.

If that is a concern you can keep track of the elapsed time in a tick method that you schedule in your Scene or Layer.

MyLayer.h

@interface MyLayer : Layer {
  ccTime totalTime;
}

@property (nonatomic, assign) ccTime totalTime;

MyLayer.m

-(id)init 
{
    if( (self = [super init]) )
    {
        [self schedule:@selector(update:)];
    }

    return self;
}

// deltaTime is the amount of running time that has passed
// since the last time update was called
// Will only be called when the director is not paused
// and when it is part of the active scene
-(void)update:(ccTime)deltaTime
{
    totalTime += deltaTime;
}
James Johnson