2011-06-24 10 views
0

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

私はシステムをセットアップしましたが、監督はそれがより良くできると思っています。これは私がy'allsの助けが必要なところです!

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

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

は今ここに私が使っていたコードです:メインGameStateで

私のJSでGameStateの更新機能

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

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

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)

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

+0

何を、正確に、スーパーバイザは思うんが、より良い行うことができますか?私たちはパフォーマンス、ビジュアル、可読性のためのコード構造などを話していますか? –

+0

@Levi Morrison:パーツパフォーマンス、パーツコード構造。それはちょうど最善の方法で設定されていません。 – Briz

+0

確かに、それは動作しますが、より良いかもしれません。私がもっと良い方法を知っていれば、経験豊富なコーダーがこれに助けてもらう必要はありません。 – Briz

答えて

0

アルゴリズムの改良

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

JS改善(あまり良く、本当に)

  • 使用===/!== istead。より具体的にできる場合は、truthyのものも避けてください。
  • 非表示通知を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); 
     } 
    } 
} 
関連する問題