views:

125

answers:

1

I have a level class and a Enemy_control class that is based off an vector that takes in Enemys as values. in my level constructor I have:

Enemy tmp( 1200 );
enemys.Add_enemy( tmp ); // this adds tmp to the vector in Enemy_control

enemys being a variable of type Enemy_control. My program crashes after these statements complaining about some destructor problem in level and enemy_control and enemy.

Level1::Level1() : Levels()
{
bgX1 = -60; // -60
bgX2 = -130; // -110
bgX3 = -240; // -
bgY=0; // is this used anymore?

// characterY=330;
max_right_movement=500;
max_left_movement=300;

 // More test
jump_max = 110;
player_current_y = 340;
jump_spd = 4;
player_current_floor_y = 340;
//CONST_LEVEL1_MAIN_Y = new int( 340 );

scrolling = true;
scrolling_right = true; // this var is in levels

level_alive = true;
restart_level = false;

player_level_x = 300;
player_screen_x = 300;

level_end_point = 1035 * 10;
level_start_point = 0;

// create enemys in the level
Enemy tmp( 1200 );
enemys.Add_enemy( tmp );


//    tmp.Set_enemy( 4600 );
//    enemys.Add_enemy( tmp );
scoreTitle = new MyText(25);
score = new MyText(25);
high_score=0;
//onblock=0;

load( "grafx/level1/clouds.png", "grafx/level1/mountain.png", "grafx/level1/ground.png", "sounds/level1music.ogg" );
}

and enemy.h:

/* Enemy.h
* obg
* 1-13-10
*/

#ifndef ENEMY_H
#define ENEMY_H

#include "Character.h"
#include <vector>

class Enemy : public Character
{
    private:
        int enemy_speed,
            DRAW_X,
            spawn_x,
            distance_to_enemy,
            frame_left,
            frame_right,
            death_frame_x,
            death_frame_y;

        int bullet_fire_rate;

        bool following_player;

    private:
        void Draw_enemy_going_right( SDL_Surface *video_surface );
        void Draw_enemy_going_left( SDL_Surface *video_surface );
        void Draw_death( int bgX3, int y, SDL_Surface *video_surface );

        void Move_frame_left();
        void Move_frame_right();

        void Draw_me( int bgX3, int y, SDL_Surface *video_surface, int player_level_x, int player_screen_x );

        void Fire_bullet();

    public:
        Enemy( int spawn_x );
        ~Enemy();

        //void Set_enemy( int spawn_x );
        void Draw( int bgX3, int y, SDL_Surface *video_surface, int player_level_x, int player_screen_x );
        bool Following_player();

};

class Enemy_control
{
    private:
        vector< Enemy > enemy_vector; // change this to a vector

    public:
        Enemy_control();
        ~Enemy_control();

        void Add_enemy( Enemy a_enemy );
        void Draw( int bgX3, int y, SDL_Surface *video_surface, int player_level_x, int player_screen_x );
        //void Free_list_from_memory();
};

Enemy constructor:

Enemy::Enemy( int spawn_x ) : Character() {

character_image.load( "grafx/enemy1.gif" );

health = 400;
damage = 20;

DRAW_X = spawn_x;
this->spawn_x = spawn_x;

distance_to_enemy = 230;
enemy_speed = 3;
frame_left = 0;
frame_right = 0;
death_frame_x = 390;
death_frame_y = 0;

following_player = false;
bullet_fire_rate = 0;

sprite_info.w = 63;
sprite_info.h = 74;
cout << "enemy built please call Set_enemy()\n";

}

Any ideas?

A: 

As alread pointed out the description of your problem is not really precise. One of the things which strikes me as odd is why the destructor of any of your classes gets called. Looking at your code it becomes clear that Enemy tmp is a local instance which gets cleaned up after leaving the Level constructor. The Add_Enemy() function receives a copy of this local object. Since you provide no copy constructor the call to character_image.load( "grafx/enemy1.gif" ); will not be executed within the copy constructor and therefor the Enemy objects you store will have no image.

Maybe you should consider something like this:

Enemy* tmp = new Enemy( 1200 ); // dynamic allocation of Enemy
enemey.Add_enemy( tmp );       

If you do someting like this you need to change your vector to store pointers to Enemy objects like std::vector< Enemy* > enemies. Do not forget to delete the dynamically allocated objects within the destructor of Enemy_control.

Holger Kretzschmar