Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

Whats wrong here!*unanswerd*

This topic is 4969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok this is just getting me ticked i see no reason this code is malfunctioning but here it is
vector<Enemy*> V_Enemys;
string Enemy_List[0]= "Enemy_1.bmp";
const char* Enemy_List_C;
SDL_Surface* I_Enemy=NULL;
string Master_Enemy_Files[] = {"Enemy_One.txt"}; 
//later in code


 for(int i=-1; i < One->Enemy_Vector.size(); i++)
         {
             Enemy_List_C = Enemy_List[0].c_str();
             I_Enemy = SDL_LoadBMP(Enemy_List_C);
             Enemy* New_Enemy = new Enemy(Master_Enemy_Files,50,50,&V_Enemys,0);
             delete New_Enemy;
            if(V_Enemys == NULL)
             {
                 asprintf(&msg,"enemy creation failed",SDL_GetError ());
              };    
            TempRectBlit(I_Enemy,screen,V_Enemys->R_Enemy);    
         }; 






thats the main file heres the Enemy constructor that is being called :
Enemy::Enemy(string File_Name[], int x, int y,vector<Enemy*> *V_Enemy,int NewID)
{
    this->ID = NewID;
    this->FileName = File_Name[ID].c_str();
    R_Enemy.x = x;
    R_Enemy.y = y;
    ifstream fin;
    this->Name = "";
    fin.open(FileName);
    getline(fin, Name);
    fin >> this->Life;
    getline(fin, Image);
    fin >> this->Weapon_ID; // index to an array;
    fin >> this->Speed;
    fin.close();
    V_Enemy->push_back(this);
};        
    






thanks edit oo gosh i do that alot heres your info error (runtime): Image isnt showed at coordinanths(50,50) as it should be it isnt showed at all, all other images are shown everything else works fine; c++ dev-cpp [Edited by - raptorstrike on November 10, 2004 8:58:49 PM]

Share this post


Link to post
Share on other sites
Advertisement
oo gosh i do that alot heres your info

error (runtime): Image isnt showed at coordinanths(50,50) as it should be it isnt showed at all, all other images are shown everything else works fine; c++ dev-cpp

Share this post


Link to post
Share on other sites
You delete the Enemy right after creating it. You seem to be counting on a copy of it being made and added to the vector, but this is a bad idea in general. Constructors are just supposed to, well, construct the object. Anyway, you have a vector of Enemy *, which is probably a good idea, but after you delete Enemy, where do you suppose the pointer in the vector is pointing to? o_O

Share this post


Link to post
Share on other sites
Quote:
Enemy* New_Enemy = new Enemy(Master_Enemy_Files,50,50,&V_Enemys,0);
delete New_Enemy;


Right there, move the delete to when youre done with the newed object.

Share this post


Link to post
Share on other sites
do one thing.just make a one function which cleanup the all sprites tht u saved in vector.and start from begin to end.and clean up tht sprite means enemy in urcase.i think it will help to u.try it out.may be u have to change ur code for some level but i think u will do this.



void CleanupSprites()
{
// Delete and remove the sprites in the sprite vector
vector<Sprite*>::iterator siSprite;
for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)
{
delete (*siSprite);
m_vSprites.erase(siSprite);
siSprite--;
}
}

Share this post


Link to post
Share on other sites
deriving from the last post, you could change this:

for(int i=-1; i < One->Enemy_Vector.size(); i++)
{
Enemy_List_C = Enemy_List[0].c_str();
I_Enemy = SDL_LoadBMP(Enemy_List_C);
.....

to this:

for(int i=0; i < One->Enemy_Vector.size(); i++)
{

I_Enemy = SDL_LoadBMP(Enemy_List.c_str());
.....


unless i missed something

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
deriving from the last post, you could change this:

..
Enemy_List_C = Enemy_List[0].c_str();
..

to this:

..
I_Enemy = SDL_LoadBMP(Enemy_List.c_str());
..


unless i missed something


Alpha, it doesn't look like that's what he wants to do. Enemy_List is an array of strings (containing only one string). It looks like he wants to load that same bmp multiple times.

raptorstrike, you don't need to load it every time in the loop. You can just load it once and use it multiple times (at least, it doesn't look to me like you require that many unique copies of it).

Also you don't need to pass the vector into your Enemy class. Just push it onto the vector in your loop.

You should also follow Endurion's comments about not starting at -1.

You don't seem to use One->Enemy_Vector anywhere; is your code correct? Your code doesn't look very well thought out.. :-)

cheers
sam

Share this post


Link to post
Share on other sites
ok there is alot about my code that is diffrent and for right now is a little confusing like the whole [0] think in some of my arrays thats just their because zero is the ONLY part ive deffined really this will later turn into [rand()%X] were x is the # of elements in the array as far as the whole One->Enemy_Vector i thought i took it out but i guess not. Now i want a new object (Enemy*) to be created every loop when i < One->Respawn (yes respawn is > 0) but my problem is this

for(int i=0; i < One->Respawn; i++)
{
Enemy_List_C = Enemy_List[0].c_str();
I_Enemy = SDL_LoadBMP(Enemy_List_C);
Enemy* New_Enemy = new Enemy
(Master_Enemy_Files,50,50,&V_Enemys,0);//when loop repeats wont this be over written then what will the pointer in the vector point to?
One->Respawn++;
if(V_Enemys == NULL)
{
asprintf(&msg,"enemy creation failed",SDL_GetError ());
};
TempRectBlit(I_Enemy,screen,V_Enemys->R_Enemy);
};



as you can see ive fixed a few thing but i dont know what to do about the whole wild pointer thing. all i want is for a new object to be created and added to a vector when the loop is fufilled shoud i go ahead and make a function? also about the code it probibly makes less sience because its in fragments also when i do release the source it will be better looking and with more comments as of right now this is what i got

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!