0

主に C++ と Javascript を使用する 3D 学習ベースのゲームに取り組んでいます。プレイヤーがノートブックに情報を送信したときの通知システムを設計しようとしています。

システムをセットアップしましたが、スーパーバイザーは改善できると考えています。ここで皆さんの助けが必要です!

それが行った非常に基本的な方法:

プレイヤーは、ノートブックに送信される情報をトリガーする何かを行います。これが起こったのと同じ方法で、通知をオンにしました。通知は、画像の 2 つの div を点滅させる (点滅効果を作る) ことによって、プレーヤーの画面に表示されます。これらの div のいずれかがクリックされると、プレーヤーにノートブックが表示されます。プレーヤーがノートブックを表示または終了すると、通知はオフになります。

今ここに私が使用していたコードがあります:

メインの GameState で

int GameModeState::notify(int query)
{
    static int notified;
    if(query == 1)
    {
        notified = 1;
        return notified;
    }
    if(query == 2)
    {
        notified = 0;
        return notified;
    }
    else
    {
        return notified;
    }
}

GameState の update 関数で

// checks if the unviewed information notification needs to be on or off
if(notify(0) == 1) // supposed to be on
{
    mScreen->executeJavascript("notebookNotification(1);"); // turns it on
} else {
    int nothing = notify(2); // clears out notify to 0
    mScreen->executeJavascript("notebookNotification(0);"); // turns it off
}

私のJSで

var intervalID; // Needed to turn off setInterval()
//Function takes in 0 to turn off notification, anything else turns it on
function notebookNotification(setting)
{
   if(setting == 0)
   {
      if(intervalID) {
        // clears the blinking darkContainer
        window.clearInterval(intervalID);
        intervalID = null;
    }
    // hides both of the images
    $("#lightNotificationContainer").hide();
    $("#darkNotificationContainer").hide();
}
else
{
    $("#lightNotificationContainer").show();
    if(!intervalID) {
        // "animates" the blinking of the notification; the darkContainer toggles on and off every second, and covers the lightContainer
        intervalID = window.setInterval('$("#darkNotificationContainer").toggle()', 1000);
    }
}
}

を使用して通知をオフにしますGameModeState::notify(2)

では、これよりも優れたシステムは何でしょうか?

4

1 に答える 1

0

アルゴリズムの改善

  • 静的フラグを使用しないでください。通知を一意にターゲットできるように、ID システムを作成します。
    • C++ では、新しい通知を行うたびに自動インクリメントする変数を追跡できます。#notification_#ID は、必要な ID の場所である可能性があり#ます。次に、通知機能は、停止/開始するIDと、開始または停止するパラメーターを送信します。
    • JavaScript では、間隔の作成からの ID をタグに埋め込みます。.data()の使用をお勧めします。そうすればオフにできます。

JS の改善 (実際にはあまり良くありません)

  • ほとんどの場合、 ===/!==の代わりに==/を使用します。!=また、より具体的にできる場合は、真実のものを避けてください。
  • 非表示通知を 1 つのクエリに結合しました。

コード:

var intervalID; // Needed to turn off setInterval()
//function takes in 0 to turn off notification, anything else turns it on

function notebookNotification(setting) {
    if (setting === 0) {
        if (intervalID !== null) {
            // clears the blinking darkContainer
            window.clearInterval(intervalID);
            intervalID = null;
        }
        // hides both of the images
        $("#lightNotificationContainer,#darkNotificationContainer").hide();
    }
    else {
        $("#lightNotificationContainer").show();
        if (intervalID === null) {
            // "animates" the blinking of the notification; the darkContainer toggles on and off every second, and covers the lightContainer
            intervalID = window.setInterval('$("#darkNotificationContainer").toggle()', 1000);
        }
    }
}
于 2011-06-24T23:23:47.767 に答える