Ey和
我的游戏需要一些帮助。我正在尝试更改介绍类中的游戏状态,但它会产生很多错误。
Intro.h
#pragma once
#include "SpriteBatch.h"
#include "GameState.h"
class Intro
{
public:
Intro();
~Intro();
void Update(GameState* gameState);
void Draw(DirectX::SpriteBatch* spriteBatch);
private:
float _timer = 0.0f;
};
Intro.cpp
#include "Intro.h"
#include "LoadContent.h"
#include "SimpleMath.h"
Intro::Intro()
{
LoadContent::InitIntro();
if (!LoadContent::isLoaded("Block"))
LoadContent::LoadTexture(L"Images/Block.dds", "Block");
}
Intro::~Intro()
{
}
void Intro::Update(GameState* gameState)
{
if (_timer < 10)
_timer += 0.1;
else
gameState->SwitchState(GameState::ScreenType::MenuScreen);
}
void Intro::Draw(DirectX::SpriteBatch* spriteBatch)
{
spriteBatch->Draw(LoadContent::GetTexture("Block"), DirectX::SimpleMath::Vector2(170, 196), DirectX::Colors::White);
}
GameState.h
#pragma once
#include "SpriteBatch.h"
#include "Menu.h"
#include "SwitchScreen.h"
#include "Intro.h"
class GameState
{
public:
GameState();
~GameState();
void Update();
void Draw(DirectX::SpriteBatch* spriteBatch, DirectX::SpriteFont* spriteFont);
public:
enum ScreenType{Direct, MenuScreen, Game};
private:
ScreenType _screenType;
Intro* _screen;
Menu* _menu;
SwitchScreen* _switch;
public:
void GameState::SwitchState(ScreenType switchtype);
};
GameState.cpp
#pragma once
#include "GameState.h"
GameState::GameState()
{
_switch = new SwitchScreen();
_screenType = ScreenType::Direct;
SwitchState(_screenType);
}
GameState::~GameState()
{
}
void GameState::Update()
{
_switch->Update();
switch (_screenType)
{
case GameState::Direct:
_screen->Update(this);
break;
case GameState::MenuScreen:
_menu->Update();
break;
case GameState::Game:
break;
default:
break;
}
}
void GameState::Draw(DirectX::SpriteBatch* spriteBatch, DirectX::SpriteFont* spriteFont)
{
switch (_screenType)
{
case GameState::Direct:
_screen->Draw(spriteBatch);
break;
case GameState::MenuScreen:
_menu->Draw(spriteBatch, spriteFont);
break;
case GameState::Game:
break;
default:
break;
}
_switch->Draw(spriteBatch);
}
void GameState::SwitchState(ScreenType switchtype)
{
if (!_switch->GetUpdate())
{
_switch = new SwitchScreen();
}
else {
if (_switch->GetUpdate())
{
_screen = NULL;
_menu = NULL;
switch (switchtype)
{
case GameState::Direct:
_screen = new Intro();
break;
case GameState::MenuScreen:
_menu = new Menu();
break;
case GameState::Game:
break;
default:
break;
}
//_switch->SetSwitch(false);
}
}
}
错误1错误C2061:语法错误:标识符 'GameState'\ jelly \ intro.h 11
错误2错误C2061:语法错误:标识符 'GameState'\ jelly \ intro.h 11
错误6错误C2061:语法错误:标识符 'GameState'\ jelly \ intro.h 11
错误3错误C2143:语法错误:缺少';'之前 '*'\ jelly \ gamestate.h 23
错误7错误C2660: '简介::更新':功能不带1 arguments \ jelly \ gamestate.cpp 24
错误4错误C4430: 缺少类型说明符 - 假设为int。注意:C ++不支持 default-int \ jelly \ gamestate.h 23
警告5警告C4305:'+ =' :从'double'截断到 'float'\ jelly \ intro.cpp 21
谢谢
答案 0 :(得分:0)
看起来像一个循环包含问题。
您应该尝试在GameState
头文件
Intro.h
<强> Intro.h
强>
#include "SpriteBatch.h"
// #include "GameState.h" <<< Nope!
class GameState; // Do this instead
而非在GameState.h
中加入Intro.h
。
相应地在GameState.h
中加入Intro.cpp
,以便在那里看到完整的成熟类声明:
<强> Intro.cpp
强>
#include "Intro.h"
#include "LoadContent.h"
#include "SimpleMath.h"
#include "Gamestate.h" // <<<<
答案 1 :(得分:0)
1。您在GameState.h
中不需要Intro.h
。
<强> Intro.h
强>
#pragma once
#include "SpriteBatch.h"
//#include "GameState.h" -> remove this
class GameState; //Add this line - forward declaration is enough to declare a pointer.
2。将GameState.h
添加到Intro.cpp
。
<强> Intro.cpp
强>
#include "Intro.h"
#include "LoadContent.h"
#include "SimpleMath.h"
#include "GameState.h" //add this
3。您Intro.h
中不需要GameState.h
。
<强> GameState.h
强>
#pragma once
#include "SpriteBatch.h"
#include "Menu.h"
#include "SwitchScreen.h"
//#include "Intro.h" -> remove this
class Intro; //Add this line - forward declaration is enough to declare a pointer.
4. 。将Intro.h
添加到GameState.cpp
。
<强> GameState.cpp
强>
#pragma once
#include "GameState.h"
#include "Intro.h" //add this
5. 最大的问题。
在GameState
课程中,更改:
public:
void GameState::SwitchState(ScreenType switchtype);
到
public:
void SwitchState(ScreenType switchtype);
这就是你的编译器可能在抱怨的事情。