我有一个游戏从左边的玩家右侧射击坏人。
随着游戏时间的增加,我希望我的敌人的生成率变得更快。
我在初始版和初版中设置了一个双timeOfStart = CACurrentMediaTime();
。更新方法中的NSLog(@"time is %d", timeOfStart + dt);
。
但我得到的价值如下:
time is 1581741008
time is 863073232
time is -1024003120
time is -1390701616
time is 14971856
为什么我会得到大值,然后更小,然后是负值!?
答案 0 :(得分:0)
CACurrentMediaTime()返回double值。
所以
NSLog(@"time is %f", timeOfStart + dt);
您可以通过两种方式跟踪时间:
使用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];
}
手动追踪时间:
//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;
}
}