Hello. I have been at this for a week and it still confuses me.
Basically, I have a class, class_bot, coded in a .h file. It also has some inheritance of another class, class_physics. I am only bringing that up because I think it's relevant.
Anyway, my problem is this: I have a different class, class_map, which is meant to allocate a Dynamic Array of the class_bot class. It does this like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
class class_map
{
private:
SDL_Surface* BMP_bot;
class_bot *bot;
......................
void class_map::load()
{
bot = new class_bot[7];
if (bot)
{
BMP_bot = IMG_Load("data/en_bot.png");
for (int i = 0; i < 6; i++)
{
bot[i].create(i*40, 20); //This basically sets its x and y positions
bot[i].load(BMP_bot); //This passes a pointer to the sprite it draws.
}
}
}
.......................
void class_map::draw( SDL_Surface* BMP )
{
for (int i = 0; i < 5; i++)
{
bot[i].step(room); //Draw
}
}
..............................
void class_map::destroy( )
{
delete []bot;
if(BMP_bot!=NULL) SDL_FreeSurface(BMP_bot);
}
|
So basically, all I'm trying to do is allocate an array of 7 bots, and draw only 5 of those (I was attempting to use trial and error earlier).
However, no matter what I do, everytime I run the game either a random amount of bots show up, they perform actions which indicate their 'gravity' value was not properly set, despite it being set in their constructor, or the game refuses to open alltogether and just crashes. I know this is because of the memory allocation, but I honestly don't know why this is happening. Somebody, please help. Thankyou.
EDIT: Also, when no dynamic memory allocation is used, the bots operate perfectly every time.