2011-06-24 29 views
0

我正在研究一種主要利用C++和Javascript的基於3D學習的遊戲。我試圖設計一個通知系統,當玩家將信息發送到他們的筆記本時。如何在遊戲中設置通知系統

我建立了一個系統,但是主管認爲它可以做得更好。這是我需要你們幫忙的地方!

非常基本的方式,去:

玩家會做一些觸發信息發送到筆記本電腦。在發生這種情況的相同方法中,我打開了通知。通知會通過閃爍兩個div的圖像(閃爍效果)顯示在玩家的屏幕上。當其中任一個div被點擊時,它向玩家顯示筆記本。任何時候玩家查看或退出筆記本,通知都會關閉。

現在,這裏是我使用的代碼:

在主遊戲狀態

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

在遊戲狀態的更新功能

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

現在,什麼是更好的系統使用,而不是這個?

+0

究竟什麼不主管認爲可以做的更好?我們在談論性​​能,視覺效果,可讀性代碼結構,什麼? –

+0

@Levi Morrison:部分性能,部分代碼結構。它只是沒有以最好的方式建立。 – Briz

+0

當然,它有效,但它可能會更好。如果我知道一個更好的方法,我不會問經驗豐富的編碼員對此有所幫助。 – Briz

回答

0

算法的改進

  • 不要使用靜態標誌。創建一個ID系統,以便您可以唯一地定位通知。
    • 在C++中,您可以跟蹤每次發出新通知時自動遞增的變量。該ID可能是#notification_#其中#是您想要的標識。然後你的通知函數將發送它想要停止/啓動的id,以及參數來啓動或停止它。
    • 在JavaScript中,您隨後在標記中嵌入了創建間隔的id。我建議使用.data()。這樣你就可以關閉它。的==/!=在大多數情況下

JS改進(也好不了多少,真的)

  • 使用===/!== istead。如果你可以更具體的話,也要避免truthy的東西。
  • 將隱藏通知合併到一個查詢中。

代碼:

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