1

私は C# プログラミングの初心者で、XNA で使用するのはこれが初めてです。友達と一緒にゲームを作ろうとしていますが、基本的なカウンター/クロックの作成に苦労しています。必要なのは、1 から始まり、2 秒ごとに +1 で、最大容量が 50 のタイマーです。ありがとう。

4

2 に答える 2

3

XNA でタイマーを作成するには、次のようなものを使用できます。

int counter = 1;
int limit = 50;
float countDuration = 2f; //every  2s.
float currentTime = 0f;

currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds; //Time passed since last Update() 

if (currentTime >= countDuration)
{
    counter++;
    currentTime -= countDuration; // "use up" the time
    //any actions to perform
}
if (counter >= limit)
{
    counter = 0;//Reset the counter;
    //any actions to perform
}

私は C# や XNA の専門家でもありませんので、ヒントや提案をいただければ幸いです。

于 2012-11-15T10:05:12.143 に答える
-1

XNA ElapsedTime を使用したくない場合は、c# タイマーを使用できます。それに関するチュートリアルを見つけることができます。タイマーの msdn リファレンスはこちら

とにかく、多かれ少なかれあなたが望むことをするいくつかのコードがあります。

まず、クラスで次のように宣言する必要があります。

    Timer lTimer = new Timer();
    uint lTicks = 0;
    static uint MAX_TICKS = 50;

次に、好きな場所でタイマーを初期化する必要があります

    private void InitTimer()
    {
        lTimer       = new Timer();
        lTimer.Interval = 2000; 
        lTimer.Tick += new EventHandler(Timer_Tick);
        lTimer.Start();
    }

次に、Tick イベントハンドラーで、50 ティックごとにやりたいことを実行する必要があります。

    void Timer_Tick(object sender, EventArgs e)
    {
        lTicks++;
        if (lTicks <= MAX_TICKS)
        {
            //do whatever you want to do
        }
    }

お役に立てれば。

于 2012-11-15T10:09:48.487 に答える