我的倒计时每秒使用每帧更新的InGameTime
值进行更新。
这在视觉上很有效,因为我可以将游戏时间缩小到最接近的int。
...但是如何让我的应用在每秒钟后发出一声嘟嘟声?
以下是我的代码:
-(void) setTimer:(ccTime) delta{
int timeInSeconds = (int)appDelegate.InGameTime;//Game time E.G: 8.2332432
/*
Stuff is here to display the tempTime
*/
//The below effect plays the sound every frame,
//is there an equation that I can apply to appDelegate.InGameTime
//that will play it once a second?
[[SimpleAudioEngine sharedEngine] playEffect:@"tick.mp3"];
}
答案 0 :(得分:2)
-(void) setTimer:(ccTime) delta{
static float timeSinceLastTick = 0.f;
int timeInSeconds = (int)appDelegate.InGameTime;//Game time E.G: 8.2332432
/*
Stuff is here to display the tempTime
*/
// if your delta is small and relatively constant, this
// should be real close to what you want.
// other ways exist
timeSinceLastTick += delta;
if (timeSinceLastTick > 1.0f) {
timeSinceLastTick=0.f;
[[SimpleAudioEngine sharedEngine] playEffect:@"tick.mp3"];
}
}
答案 1 :(得分:1)
您可以这样做的一种方法是跟踪方法调用之间的时间量并将其与InGameTime相关联。
e.g。
- (void)setTimer:(ccTime) delta
{
int timeInSeconds = (int)appDelegate.InGameTime;//Game time E.G: 8.2332432
/*
Stuff is here to display the tempTime
*/
//The below effect plays the sound every frame,
//is there an equation that I can apply to appDelegate.InGameTime
//that will play it once a second?
// beepTime is a float instance variable at first initialized to appDelegate.InGameTime - floor(appDelegate.InGameTime)
beepTime += delta;
if (beepTime >= 1.0f) // where 1.0f is the frequency of the beep
{
[[SimpleAudioEngine sharedEngine] playEffect:@"tick.mp3"];
beepTime = appDelegate.InGameTime - floor(appDelegate.InGameTime); // should return a decimal from 0.0f to 1.0f
}
}
我认为应该有效。让我知道。
答案 2 :(得分:0)
为什么不使用计划方法?
您只需指定间隔,然后将要触发的代码放在块
中//call startBeep function every 1 second
[self schedule:@selector(startBeep:) interval:1.0f];
- (void)startBeep:(ccTime) delta
{
[[SimpleAudioEngine sharedEngine] playEffect:@"tick.mp3"];
}