diff --git a/cocos/platform/ios/CCDirectorCaller-ios.mm b/cocos/platform/ios/CCDirectorCaller-ios.mm index c5a8d11d0d..1582eb8a7e 100644 --- a/cocos/platform/ios/CCDirectorCaller-ios.mm +++ b/cocos/platform/ios/CCDirectorCaller-ios.mm @@ -26,6 +26,8 @@ #include "platform/CCPlatformConfig.h" #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS +#include + #import "platform/ios/CCDirectorCaller-ios.h" #import @@ -51,7 +53,7 @@ static id s_sharedDirectorCaller; { if (s_sharedDirectorCaller == nil) { - s_sharedDirectorCaller = [CCDirectorCaller new]; + s_sharedDirectorCaller = [[CCDirectorCaller alloc] init]; } return s_sharedDirectorCaller; @@ -64,20 +66,17 @@ static id s_sharedDirectorCaller; s_sharedDirectorCaller = nil; } --(void) alloc -{ - interval = 1; -} - (instancetype)init { - self = [super init]; - if (self) + if (self = [super init]) { isAppActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(appDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil]; [nc addObserver:self selector:@selector(appDidBecomeInactive) name:UIApplicationWillResignActiveNotification object:nil]; + + self.interval = 1; } return self; } @@ -104,6 +103,10 @@ static id s_sharedDirectorCaller; // Director::setAnimationInterval() is called, we should invalidate it first [self stopMainLoop]; + // initialize initLastDisplayTime, or the dt of of first frame is invalid + // should init before start displayLink + [self initLastDisplayTime]; + displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)]; [displayLink setFrameInterval: self.interval]; [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; @@ -143,6 +146,16 @@ static id s_sharedDirectorCaller; } } +-(void)initLastDisplayTime +{ + struct mach_timebase_info timeBaseInfo; + mach_timebase_info(&timeBaseInfo); + CGFloat clockFrequency = (CGFloat)timeBaseInfo.denom / (CGFloat)timeBaseInfo.numer; + clockFrequency *= 1000000000.0; + // convert absolute time to seconds and should minus one frame time interval + lastDisplayTime = (mach_absolute_time() / clockFrequency) - ((1.0 / 60) * self.interval); +} + @end #endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS