Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Spawning Multiple Objects Randomly

$
0
0
Hi,Ive been trying to make multiple objects from the same class and spawn them randomly in the screen but i run into a problem.
First here is the code(btw iam using SDL)
the class
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
    


The main()
int main( int argc, char* args[] )
{
    
       
        init(); 
        load_files();
        
       
        Air *myAir = new Air;   
        vector<Astro> act;
        
        for(int n = 0;n<5;n++)
        {
               Astro *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;
}



The problem here is that five objects spawn but in the same place/spawn point :hmmm:/>/>
Q1:What iam doing wrong here?is it the way i used vector?
Q2:What is the proper way to do it?need some good Explanation so i can solve it.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>