-1

我有一个游戏,从右边向左边的玩家射击坏人。

我希望我的敌人的生成速度随着游戏时间的增加而变得更快。

timeOfStart = CACurrentMediaTime();在 init 和NSLog(@"time is %d", timeOfStart + dt);update 方法中设置了一个 double 。

但我得到的值如下:

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

为什么我得到大值,然后更小,然后是负值!?

4

1 回答 1

0

CACurrentMediaTime() 返回双精度值。

所以

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;
              }  
        }
    
于 2013-04-13T05:35:33.300 回答