novice programmer here, I'm using c++ with the SFML library, Code::Blocks, and the GNU GCC Compiler. Windows XP.
I'm making a simple platformer as practice for a larger game in the future. I decided to set it up so that it reads all of the level data from a text file. I just added enemies to the program (although right now it just prints a sprite on screen) and it worked fine the first time, but after that it crashes during runtime. Here's the relevant bits of code:
Enemy.h:
class Enemy
{
...
}
typedef Enemy* EnemyPointer;
Main.h:
class App
{
private:
sf::Image EnemyImage;
EnemyPointer TheEnemies;
int LevelData[256][4];
public:
App();
int Execute();
bool Init();
void Cleanup();
}
Main.cpp:
App::App()
{
TheEnemies = new Enemy[16]
}
int main()
{
App TheApp;
return TheApp.Execute();
}
int App::Execute()
{
if(!Init()) return -1;
LoadLevel();
while(Running)
{
OnEvent(TheEvent);
Loop();
Render();
Frame++;
}
Cleanup();
return 0;
}
Init.cpp:
bool App::Init()
{
if(!EnemyImage.LoadFromFile("Enemy.bmp"))
{
std::cout<<"Failed to load \"Enemy.bmp\"";
return false;
}
//Program crashes in this for loop.
for(int c=0;c<=LevelData[0][1];c++)
{
TheEnemies[c].SetX(LevelData[LevelData[0][0]+1+c][0]);
TheEnemies[c].SetY(LevelData[LevelData[0][0]+1+c][1]);
TheEnemies[c].SetType(LevelData[LevelData[0][0]+1+c][2]);
TheEnemies[c].Sprite.SetImage(EnemyImage);
}
}
Cleanup.cpp:
void App::Cleanup()
{
delete[] TheEnemies;
}
LevelData[][] is a 2D array containing the information that's read from the .txt file. It contains the number of enemies, their coordinates, and their types.
I've determined that the program crashed during Init() when it tries to use the array of Enemies that was created in the constructor. I marked it with a comment. I've looked up forum posts about how to create an array of objects and most of them say this is how to do it. I can't figure out why it's crashing. If anyone know how to fix this it would be greatly appreciated. Thanks.