2013-03-01 63 views
0

我試圖允許在我的遊戲中暫停。目前,我有一個很好的改變狀態。對於暫停我有:遊戲狀態管理器,暫停和恢復

void PushState(int newState) 
{ 
    pauseID = stateID; ///save state number 

    Gstates.push_back(currentState); ///Set Current state to vector 

    nextState = newState; ///acquire new state 

    switch(nextState) 
    { 
    case STATE_INTRO: 
     currentState = new CIntroState(); ///create new state 
     break; 
    } 

    //Change the current state ID 
    stateID = nextState; 

    //NULL the next state ID 
    nextState = STATE_NULL; 
} 

以上部分似乎工作正常。

這是我的簡歷部分

void Resuming() 
{ 
    nextState = pauseID; 

    if(nextState != STATE_EXIT) 
    { 
     delete currentState; ///deletes current state 
    } 

    switch(nextState) 
    { 
    case STATE_INTRO: 
     currentState = Gstates.back(); ///sets current state to the saved state 
     Gstates.pop_back(); ///delets saved state 
     break; 
    } 

    //Change the current state ID 
    stateID = nextState; 

    //NULL the next state ID 
    nextState = STATE_NULL; 
} 

我得到一些奇怪的多線程錯誤。大約50%的時間是按預期的方式工作的,但其餘的時間都是崩潰的。

誤差基本上說,「這很可能是一個多線程的客戶端和XInitThreads沒有被調用

客戶端是不是多線程;)..總之,沒有任何人有任何想法是怎麼回事?在

+0

http://tronche.com/gui/x/xlib/display/XInitThreads.html – Saqlain 2013-03-01 18:12:38

回答

0

你有一些危險的代碼中有這樣:

if(nextState != STATE_EXIT) 
{ 
    delete currentState; ///deletes current state 
} 

將離開你「currentState」指向不存在的對象,因爲它,纔會更新nextState是STATE_INTRO:

switch(nextState) 
{ 
case STATE_INTRO: 
    currentState = Gstates.back(); ///sets current state to the saved state 
    Gstates.pop_back(); ///delets saved state 
    break; 
} 

這很可能是崩潰的原因。

+0

雅,我認爲這也是導致崩潰的原因。我不知道爲什麼。整個想法是保存舊的currentState,運行新的currentState,而不是恢復到舊的currentState。也許我需要使用除矢量以外的東西來存儲對象? – Chivos 2013-03-01 18:26:51

+0

,但你回到currentState?你確定當執行Resuming時,從暫停ID恢復的'nextState'是STATE_INTRO嗎?因爲在pushState中,您將副本stateID保存爲pauseID,並且如果我正確理解邏輯,則它是運行狀態的ID,因此可能是_not_ STATE_INTRO。 – user2116939 2013-03-01 19:07:26

+0

我已經做了一些cout,並保持的值是pauseID,它是State_Intro。 pauseID是爲了存儲被保存狀態的值。在這種情況下,它是介紹狀態,因爲我只使用該活動狀態。我有一個櫃檯增加,所以我切換狀態,並觀看櫃檯重新開始。比恢復暫停狀態,看看計數器是否繼續。 – Chivos 2013-03-01 19:29:28