2013-03-01 78 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