3 Replies - 473 Views - Last Post: 02 February 2013 - 11:51 AM Rate Topic: -----

#1 Xwow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 13

Spawning Multiple Objects Randomly

Posted 02 February 2013 - 08:35 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Spawning Multiple Objects Randomly

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Spawning Multiple Objects Randomly

Posted 02 February 2013 - 09:28 AM

Put a breakpoint here:
15      box2.h=90;


Now look at the value of box2.x
Is it always the same? If it is, then your use of the random must be wrong because you aren't getting different values.
Was This Post Helpful? 0
  • +
  • -

#3 Xwow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 13

Re: Spawning Multiple Objects Randomly

Posted 02 February 2013 - 10:46 AM

Thanks for Reply tlhIn but no need to debug, Iam getting random values for box2.x i think you misunderstood my problem(sorry my English is not good)so I will try to explain it better.

What i wanna do is to:
1- Create multiple objects(asteroids) in the screen and have them randomly appear along the X axis.
2- Make the objects move downward along the Y axis(done it).

Now for step 1 i tried to use std::vector as u see in my code but what happens is that 5 objects are created but they all have the same box2.x Value.they appear in different locations when i rerun the program but like i said they all appear at the spot.
Was This Post Helpful? 0
  • +
  • -

#4 Xwow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 13

Re: Spawning Multiple Objects Randomly

Posted 02 February 2013 - 11:51 AM

K i solved it, i shouldve created
SDL_Rect box2;
inside the class :stupid: i think i should relearn about classes and objects,

Sorry for bothering people.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1