2

カウントダウンはInGameTime、フレームごとに更新される値を使用して1秒ごとに更新されます。

ゲーム時間を最も近い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"];

}
4

3 に答える 3

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"];
    }

}
于 2013-02-19T03:41:18.603 に答える
1

これを行う1つの方法は、メソッド呼び出し間の時間を追跡し、それをInGameTimeに関連付けることです。

例えば

- (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
    }
}

私はそれがうまくいくはずだと信じています。お知らせ下さい。

于 2013-02-19T03:39:54.493 に答える
0

スケジュール方式を使ってみませんか?

http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a7e0c230d398bba56d690e025544cb745

間隔を指定して、トリガーされるコードをブロック内に配置するだけです

//call startBeep function every 1 second
[self schedule:@selector(startBeep:) interval:1.0f];


- (void)startBeep:(ccTime) delta
{
[[SimpleAudioEngine sharedEngine] playEffect:@"tick.mp3"];
}
于 2013-02-22T10:36:16.990 に答える