-1

左にいるプレイヤーの右から悪者を撃つゲームがあります。

ゲームのプレイ時間が長くなるにつれて、敵のスポーン速度が速くなるようにしたいです。

timeOfStart = CACurrentMediaTime();initに double を設定NSLog(@"time is %d", timeOfStart + dt);し、update メソッドに a を設定しました。

しかし、私は次のような値を取得します:

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

値が大きくなり、次に小さくなり、次に負になるのはなぜですか!?

4

1 に答える 1

0

CACurrentMediaTime() は double 値を返します。

そう

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

時間を追跡するには、次の 2 つの方法があります。

  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 に答える