代码:
struct Unit
{
public:
Unit(char* name, int imgId, bool left)
{
Burning = 0;
Slowed = 0;
Area = 0;
this->Name = string(name);
ImgId = imgId;
LeftTeam = left;
}
Unit()
{
Burning = 0;
Slowed = 0;
Area = 0;
}
Unit(char* name, bool left, int unitCount)
{
Burning = 0;
Slowed = 0;
this->Name = name;
LeftTeam = left;
SetStats();
}
void Update()
{
if (Health > 0)
{
if (Slowed > 0)
{
X += (Speed * FreezeLost);
Slowed--;
}
else
X += Speed;
}
if (Burning > 0)
{
Health -= BurningDamage;
Burning--;
}
}
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
int GetArea()
{
return Area;
}
void Spawn(int x, int y)
{
X = x;
Y = y;
}
void Burn()
{
Burning = BurningRound;
}
void Freeze()
{
Slowed = FreezeRound;
}
void Damage(int dmg)
{
Health -= dmg;
if (Speed / 5 == 0)
X -= Speed/abs(Speed);
else
X -= Speed / 5;
}
int GetDamage()
{
return Attack;
}
string GetName()
{
return Name;
}
string GetDescribtion()
{
return Describtion;
}
int GetImgId()
{
return ImgId;
}
~Unit()
{}
private:
int X;
int Y;
string Name;
int Health;
int Speed;
int Attack;
bool Ranged;
int Burning;
int Slowed;
bool LeftTeam;
int ImgId;
string Describtion;
void SetStats()
{
if (strcmp(Name.c_str(), "h") == 0)
{
Health = 100;
Speed = 5;
Attack = 15;
Ranged = true;
ImgId = LeftTeam ? 0 : unitCount;
Describtion = "asd;
}
else if (strcmp(Name.c_str(), "g") == 0)
{
Health = 200;
Speed = 3;
Attack = 3;
Ranged = true;
ImgId = LeftTeam ? 1 : unitCount + 1;
Describtion = "asd";
}
else if (strcmp(Name.c_str(), "f") == 0)
{
Health = 200;
Speed = 3;
Attack = 3;
Ranged = true;
ImgId = LeftTeam ? 2 : unitCount + 2;
Describtion = "asd";
}
else if (strcmp(Name.c_str(), "e") == 0)
{
Health = 1000;
Speed = 1;
Attack = 100;
Ranged = false;
ImgId = LeftTeam ? 3 : unitCount + 3;
Describtion = "asd";
}
else if (strcmp(Name.c_str(), "d") == 0)
{
Health = 250;
Speed = 4;
Attack = 50;
Ranged = true;
ImgId = LeftTeam ? 4 : unitCount + 4;
Describtion = "asd";
}
else if (strcmp(Name.c_str(), "c") == 0)
{
Health = 300;
Speed = 3;
Attack = 30;
Ranged = false;
ImgId = LeftTeam ? 5 : unitCount + 5;
Describtion = "asd";
}
else if (strcmp(Name.c_str(), "b") == 0)
{
Health = 75;
Speed = 10;
Attack = 10;
Ranged = false;
ImgId = LeftTeam ? 6 : unitCount + 6;
Describtion = "asd";
}
else if (strcmp(Name.c_str(), "a") == 0)
{
Health = 150;
Speed = 5;
Attack = 25;
Ranged = false;
ImgId = LeftTeam ? 7 : unitCount + 7;
Describtion = "asd";
}
}
};
struct Player
{
private:
Unit* aviableUnits;
Button* gameButtons;
Button* mainMenuButtons;
bool leftPlayer;
vector<Unit> units;
int winningPercent;
int usedUnits;
int* Timers;
bool inGame;
LoadingBar* UnitCooldown;
int ActiveButtonCount()
{
int counter = 0;
for (int i = 0; i < unitCount; i++)
{
if(mainMenuButtons[i].IsSelected())
{
counter++;
}
}
return counter;
}
public:
Player()
{
aviableUnits = new Unit[0];
leftPlayer = false;
winningPercent = 0;
inGame = false;
usedUnits = 0;
}
Player(bool isLeftPlayer)
{
aviableUnits = new Unit[MaxUnitTypes];
leftPlayer = isLeftPlayer;
winningPercent = 50;
inGame = false;
usedUnits = 0;
}
void SetMainMenuButtons(Button* mainMenuButtons)
{
this->mainMenuButtons = mainMenuButtons;
mainMenuButtons[unitCount].CursorEnter();
}
Button* GetMainMenuButtons()
{
return mainMenuButtons;
}
Button* GetGameButtons()
{
return gameButtons;
}
int GetUsedUnits()
{
return usedUnits;
}
void AddUnit(char* name, bool left, int X, int Y)
{
units.push_back(Unit(name, left, unitCount));
units[units.size() - 1].Spawn(X, Y);
}
void EnterGameMode()
{
Colour c1 = Colour(240, 240, 240);
Colour c2 = Colour(102, 53, 175);
Colour c3 = Colour(230, 255, 0);
Colour c4 = Colour(255, 255, 255);
usedUnits = ActiveButtonCount();
Unit temp;
int currentButton = 0;
gameButtons = new Button[usedUnits];
UnitCooldown = new LoadingBar[usedUnits];
Timers = new int[usedUnits];
for (int i = 0; i < unitCount; i++)
{
if(mainMenuButtons[i].IsSelected())
{
if(leftPlayer)
{
gameButtons[currentButton] = Button(10 + 25 * currentButton, 40, 10, c1, c2, c3, c4, mainMenuButtons[i].GetTextAsChar());
UnitCooldown[currentButton] = LoadingBar(true, 25 * currentButton, 60, 20 + 25 * currentButton, 100, 1, Colour(), Colour(0, 255, 0), Colour(255, 0, 0));
}
else
{
gameButtons[currentButton] = Button(WindowX - 10 - 25 * currentButton, 40, 10, c1, c2, c3, c4,mainMenuButtons[i].GetTextAsChar());
UnitCooldown[currentButton] = LoadingBar(true, WindowX - (25 * (currentButton + 1) - 20), 60, WindowX - (25 * (currentButton + 1)), 100, 1, Colour(), Colour(0, 255, 0), Colour(255, 0, 0));
}
UnitCooldown[currentButton].EditMaximumValue(500);
gameButtons[currentButton].TextDisplay(false);
for (int k = 0; k < unitCount; k++)
{
if(strcmp(unitNames[k], mainMenuButtons[i].GetTextAsChar()) == 0)
Timers[currentButton] = loadingTimes[k];
}
currentButton++;
}
}
gameButtons[0].CursorEnter();
inGame = true;
}
vector<Unit>& GetUnits()
{
return units;
}
Button& currentSelectedButton()
{
if (inGame)
{
for (int i = 0; i < usedUnits - 1; i++)
{
if (gameButtons[i].IsActive())
{
return gameButtons[i];
}
}
}
if (!inGame)
cout << "Error";
return gameButtons[0];
}
void PrintGameButtons()
{
for (int i = 0; i < usedUnits; i++)
{
gameButtons[i].GetButton();
UnitCooldown[i].DrawLoadingBar();
}
}
void UpdateCooldown()
{
for (int i = 0; i < usedUnits; i++)
{
UnitCooldown[i].UpdateLoadingBarStatus(Timers[i]);
}
}
int GetUnitCount()
{
return unitCount;
}
bool IsCooldownComplete(int where)
{
if (where <= usedUnits && UnitCooldown[where].IsReady())
{
UnitCooldown[where].UpdateLoadingBarStatus(-UnitCooldown[where].GetMaximumValue());
return true;
}
return false;
}
};
struct World
{
private:
LoadingBar GameWinningState;
Player* player1;
Player* player2;
int leftArrowY;
int rightArrowY;
int CurrentlyActiveButton(Button b[], int lenght)
{
for (int i = 0; i < lenght; i++)
{
if (b[i].IsActive())
return i;
}
return -1;
}
Unit createUnit(bool left, char* name)
{
if(left)
{
player1->AddUnit(name, left, 5, leftArrowY - 20);
}
}
public:
World(Player& player1, Player& player2)
{
GameWinningState = LoadingBar(false, 5, WindowY - 5, WindowX - 5, WindowY - 20, 1, Colour(), Colour(0, 0, 255), Colour(255, 255, 0));
GameWinningState.UpdateLoadingBarStatus(50);
this->player1 = &player1;
this->player2 = &player2;
leftArrowY = 120;
rightArrowY = 120;
}
void DrawWorld()
{
gout << move_to(10, 20) << color(0, 0, 0) << text(player1->currentSelectedButton().GetTextAsChar());
gout << move_to(WindowX - gout.twidth(player2->currentSelectedButton().GetTextAsString()) - 5, 20) << text(player2->currentSelectedButton().GetTextAsChar());
player1->PrintGameButtons();
player2->PrintGameButtons();
//units
DrawArrow(leftArrowY, rightArrowY);
//end of units
GameWinningState.DrawLoadingBar();
player1->UpdateCooldown();
player2->UpdateCooldown();
}
void UpdateWorld(event ev)
{
int max1 = player1->GetUsedUnits();
int max2 = player2->GetUsedUnits();
Button* player1Buttons = player1->GetGameButtons();
Button* player2Buttons = player2->GetGameButtons();
int active1 = CurrentlyActiveButton(player1Buttons, max1);
int active2 = CurrentlyActiveButton(player2Buttons, max2);
if (ev.keycode == 97 && active1 > 0)
{
player1Buttons[active1].CursorLeave();
player1Buttons[active1 - 1].CursorEnter();
}
else if (ev.keycode == 100 && active1 != -1 && active1 < max1 - 1)
{
player1Buttons[active1].CursorLeave();
player1Buttons[active1 + 1].CursorEnter();
}
else if (ev.keycode == key_right && active2 > 0)
{
player2Buttons[active2].CursorLeave();
player2Buttons[active2 - 1].CursorEnter();
}
else if (ev.keycode == key_left && active2 != -1 && active2 < max2 - 1)
{
player2Buttons[active2].CursorLeave();
player2Buttons[active2 + 1].CursorEnter();
}
else if (ev.keycode == 119 && leftArrowY > 120)
{
leftArrowY -= 50;
}
else if (ev.keycode == 115 && leftArrowY < 520)
{
leftArrowY += 50;
}
else if (ev.keycode == key_up && rightArrowY > 120)
{
rightArrowY -= 50;
}
else if (ev.keycode == key_down && rightArrowY < 520)
{
rightArrowY += 50;
}
else if(ev.keycode == key_space)
{
if (player1->IsCooldownComplete(active1))
createUnit(true, player1Buttons[active1].GetTextAsChar());
}
else if(ev.keycode == 48)
{
if (player2->IsCooldownComplete(active2))
createUnit(false, player2Buttons[active2].GetTextAsChar());
}
}
};
一切都运行良好,但是当我尝试使用 createUnit 函数时,我在调试器中遇到了这个错误:
#0 0x463b17 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () (??:??)
#1 0x4331d2 Unit::~Unit(this=0x6cfce0, __in_chrg=<optimized out>) ---\main.cpp:568)
#2 0x433b10 World::UpdateWorld(this=0x6cfe74, ev=...) (---\main.cpp:949)
#3 0x402a55 Update(player1=..., player2=..., world=..., ev=..., InGame=@0x6cfeeb: true) (---\main.cpp:1165)
#4 0x401551 main() (---\main.cpp:1014)
首先,我认为它错过了析构函数,所以我创建了一个空的,但它没有解决我的问题。我以前从未遇到过这种类型的错误,我不完全明白,问题是什么?