Question about object spawning and deleting

Hi, I,ve been trying to create my first videogame with SDL multimedia library
and used various training websites especially at lazyfoo.net.

Iam having a problem in how to spawn an object randomly in the screen..I,ve looked in the web but iam having difficulty to understand the concept behind it, also iam having problem in how to make the object disappear after a collision.

Can someone Explain to me how to do it and show me a simple code for it?


btw this is the class for the object i wana spawn and delete when collision happen
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 Astro
{
    private:
   int  yVel;

    public:
   Astro();
   ~Astro()
   {
   }
   void move();
   void show();
  
};

Astro::Astro()
{
    
    box2.x = (int)rand() % 800;
    box2.y = 0;
       
    box2.h=90;
    box2.w=90;
       
    yVel = 0;
    
    
}
void Astro::show()
{
        apply_surface( box2.x, box2.y, Astroid, screen );
}
void Astro::move()
{
    yVel=yVel+5;
     box2.y = yVel;
      if( check_collision( box1,box2 )== true)
     { 
         yVel=yVel-5;
         box2.y = yVel;         
         i=i-1;        
                         
      }
   






Last edited on
Do you want to just delete the object? You only have one instance of Astro in your code. Or do you want to respawn it somewhere else? You could just add a boolean to your class and only draw/check for collisions if it's set to true.

If you want more than one object of the same type you'll generally need some sort of manager.
If you want more than one object of the same type you'll generally need some sort of manager.


Yes that's what i meant, I wanna keep spawning multiple Asteroids randomly in the screen.

And about deleting objects i tried creating object like this :
Astro *myAstro = new Astro;
so i can use delete operator but when i use myAstro.move(); and myAstro.show(); i get compiler error "move' has not been declared" and "show' has not been declared"

Iam still learning C++ especially (classes and objects) i looked at the net and found some examples about creating more than one object of same type but the code was too complex for me to understand it.

Iam hoping someone show me a simple code and explain it to me :(
You could use an std::vector and the boolean method I described earlier.

http://www.cplusplus.com/reference/vector/vector/

This way you can just keep adding objects to the back, and when they get destroyed you can set their "active" boolean to false. To draw and check for collisions you'd just iterate over your vector and call the show and move functions for each object.

This is obviously not a very efficient implementation, but it's the simplest I can think of.

If you want to be a bit more conservative on space you could, instead of just adding a new object to the back of the vector every time, check for an already present object with a false "active" and just replace its position/speed with the newly spawned ones.

The reason that your functions didn't work with the pointer is because you need to use the pointer function calling semantics:

1
2
myClass *pointer_to_class = new myClass;
myClass->Function(); //Note the -> instead of a dot. 


This needs to be done because the pointer needs to be dereferenced. Remember that a pointer is just an integer storing a memory location. You could also do this:

 
(*myClass).Function(); //'*' Dereferences the pointer (It "fetches" the object that the pointer points to) 
Ive been trying to use the vector method u mentioned but iam having problems.

First here is the main()
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
int main( int argc, char* args[] )
{
    
       
        init(); 
        load_files();
        
       
        Air *myAir = new Air;   
        vector<Astro> act;
        Astro*a1;
        for(int n = 0;n<5;n++)
        {
                a1 = new Astro;
                act.push_back(*a1);
                }
        vector<Astro>::iterator iter ;

        Timer fps;
    
           
  
    Mix_PlayMusic(Main,-1);

    //Start the Gameloop
    while( quit == false ) 
    {
        
       fps.start();     
           
        

        while( SDL_PollEvent( &event ) )
        {
          
            myAir->handle_input();
            

           
            if( event.type == SDL_QUIT )
            {
                
                quit = true;
            }
        }
         
        
       apply_surface( 0, 0, background, screen );
       apply_surface( 0, 0, MenuM6, screen);     
               
       life();
                  
          
     
       myAir->move();
         
        
      
        for ( iter = act.begin(); iter != act.end(); ++iter ) {
               iter->Move();
              iter->show();
              
              
             }
       myAir->show();
                        
      
   
       
       
                   
       SDL_Flip( screen );
       
       
       Gameend();       

       
        if( fps.get_ticks() < 1000 / FPS)
        {
            SDL_Delay( ( 1000 / FPS )- fps.get_ticks() );
        }
    }

   
       clean_up();

       return 0;
}


And here is the class
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 Astro
{
    private:
                  
    int  yVel;
   
    public:
     SDL_Surface *Astroid;
 
    Astro()
    {
      //Initialize          
      Astroid = load_image( "Astroid.png");  
      box2.x = ( (int)rand() % 10 ) * 80;
      box2.h=90;
      box2.w=90;
    } 
    
    
   
    void Move()
{
        
     box2.y = box2.y +1;  //moving the Astroid
    if( check_collision(box1,box2 )== true)  //if collision detected
    {
        
        
        i=i-1; //reduce the life of fighter 
          
    }
        
}          
  
    void show()
    {
         apply_surface( box2.x, box2.y, Astroid, screen );
    }      //showing the Astroid
    
   
    
    
     
};


From my observation i noticed that 5 objects was created but with the each 1 on top of each other(they just look like 1 object). its like the *a1 took just 1 value form the constructor.

How to solve this?
Last edited on
Don't use pointers and manually managed dynamic memory where it isn't warranted.
1
2
3
4
5
6
7
8
9
       Air *myAir = new Air;   
        vector<Astro> act;
        Astro*a1;
        for(int n = 0;n<5;n++)
        {
                a1 = new Astro;
                act.push_back(*a1);
                }
        vector<Astro>::iterator iter ;


There is no reason for myAir to be new'd. Nor is there a reason to leak memory like a sieve in your for loop. You're allocating memory for a new object, pushing a copy of the object into the vector and throwing away the address of the new object so that the memory you just allocated for it cannot be recovered.

1
2
3
    Air myAir;   
    vector<Astro> act;
    act.resize(5) ;    


Note: One could be tempted to use the std::vector constructor taking a number of elements to create as its first argument, but that would not result in the default constructor being called for each element, which is the behavior sought after.
Last edited on
Topic archived. No new replies allowed.