SDL游戏在加载第二级时冻结

时间:2014-04-10 21:55:32

标签: c++ sdl freeze

我的游戏的第一级构建和正确播放,当我更改源文件以使其首先加载时,我的第二级也是如此。但是,当我完成第一级并加载第二级时,游戏会冻结。具体来说,所有图像(字符,背景等)渲染都很好,但它们无法移动,并且控件没有响应(但是,调试确实显示按键仍在注册)。

我认为最有可能导致问题的是我的GameScreenManager.ccp文件。

#include "GameScreenManager.h"
#include "GameScreenLevel1.h"
#include "GameScreenLevel2.h"
#include "GameScreen.h"

GameScreenManager::GameScreenManager(SDL_Renderer* renderer, SCREENS startScreen)
{
    mRenderer = renderer;
    mCurrentScreen = NULL;

    //Ensure first screen is set up.
    ChangeScreen(startScreen);
}

GameScreenManager::~GameScreenManager()
{
    mRenderer = NULL;
    delete mCurrentScreen;
    mCurrentScreen = NULL;
}

void GameScreenManager::Update(float deltaTime, SDL_Event e)
{
    if(mCurrentScreen != NULL)
    {
        mCurrentScreen->Update(deltaTime, e);
    }

    //Check if Level 1 is completed
    CompleteCheck();
}

void GameScreenManager::Render()
{
    if(mCurrentScreen != NULL)
    {
        mCurrentScreen->Render();
    }
}

void GameScreenManager::ChangeScreen(SCREENS startScreen)
{
    //Clear up the old screen.
    if(mCurrentScreen != NULL)
    {
        delete mCurrentScreen;
    }
    GameScreenLevel1* tempScreen;
    GameScreenLevel2* tempScreen2;
    switch(startScreen)
    {
        case SCREEN_LEVEL1:
            tempScreen = new GameScreenLevel1(mRenderer);
            mCurrentScreen = (GameScreen*)tempScreen;
            tempScreen = NULL;
        break;
        case SCREEN_LEVEL2:
            tempScreen2 = new GameScreenLevel2(mRenderer);
            mCurrentScreen = (GameScreen*)tempScreen2;
            tempScreen2 = NULL;
        break;
    }
}

void GameScreenManager::CompleteCheck()
{
    if(GameScreenLevel1::complete)
    {
        ChangeScreen(SCREEN_LEVEL2);
    }
}

有人认为他们能发现问题吗?

1 个答案:

答案 0 :(得分:1)

if(GameScreenLevel1::complete)
    {
        ChangeScreen(SCREEN_LEVEL2);
    }

似乎永远都是真的。加载第二级后需要将其设置为false。