2013-04-13 71 views
-1

我有一個遊戲,在左邊的玩家的右邊射擊壞人。如何在cocos2d中跟蹤時間

隨着遊戲時間的增加,我希望我的敵人的產卵率變得更快。

我已經在更新方法中設置了一個雙重timeOfStart = CACurrentMediaTime();在我的初始化& a NSLog(@"time is %d", timeOfStart + dt);

,但我得到的值,如:

time is 1581741008 
time is 863073232 
time is -1024003120 
time is -1390701616 
time is 14971856 

爲什麼我得到很大的值,再小一點,那麼負面!?

+1

scheduleUpdate並繼續將更新方法的增量時間添加到總時間ivar – LearnCocos2D

回答

0

CACurrentMediaTime()返回double值。

所以

NSLog(@"time is %f", timeOfStart + dt); 

您可以通過兩種方式跟蹤時間:

  1. 使用NSTimeInterval

    //Declare it as member variable in .h file 
    NSTimeInterval  mLastSpeedUpdateTime; 
    
    
    //In .m init method 
    mLastSpeedUpdateTime = [NSDate timeIntervalSinceReferenceDate]; 
    
    //In update method 
    NSTimeInterval interval = [NSDate timeIntervalSinceReferenceDate]; 
    
    float diff = (interval - mLastSpeedUpdateTime); 
    
    if( diff > 5.0f) //5second 
    { 
        //here update game speed. 
        mLastSpeedUpdateTime = [NSDate timeIntervalSinceReferenceDate]; 
    } 
    
  2. 手動跟蹤時間:

    //Declare this in .h 
        float    mTimeInSec; 
    
    .m init method 
    mTimeInSec = 0.0f; 
    
        -(void)tick:(ccTime)dt 
        { 
         mTimeInSec +=0.1f; 
    
          //update gamespeed for every 10sec or on ur needs 
          if(mTimeInSec>=10.0f) 
          { 
          //update game speed 
          mTimeInSec = 0.0f; 
          } 
        } 
    
+0

「調度」並不是一個精確的解決方案,因爲它與幀速率有關。但我認爲@marciokoko的案例仍然足夠精確。 –

+0

@ReckHou,每個遊戲在cocos2d中至少有一個更新線程...! – Guru

+0

那又如何?渲染幀後,「日程表」生效。 –