Newbie question - spawning new objects

Hi folks,

Apologies for asking a really simple question in the forums, but I'm hoping someone may be able to help me. I'm basically trying to create a very simple 2D XNA game in a week, from a background of not having programmed anything more complex than a VCR since Spectrum basic - it's for a magazine article, hence the need to cut a few corners when it comes to learning...

I have a problem which - I'm sure - the solution will provide me with a Eureka moment in understanding how this is done. I've followed the excellent videos on LearnXNA.com and the built-in tutorials and have figured out how to draw sprites on to the screen and move them around. It's not much, but it's a start... How do I then add in new sprites as the game progresses: for example, after 10 seconds of game time I want to create and set an new ball in motion. I've created a class for the moving ball, but I just can't see how to create a new instance at a predefined event and then include it in the Draw method...

Any help much appreciated - and credited in the mag.




Answer this question

Newbie question - spawning new objects

  • Igor.Gladyshev

    Many thanks guys - this helps an enormous amount.


  • bflemi3

    From a motivational 'if I can do this, so can you' pov, yes.

  • Morn

    not having programmed anything more complex than a VCR since Spectrum basic - it's for a magazine article


    Why are you trying to write a magazine article on a subject you're not familiar with
    Is that really doing the readers of the magazine much of a service


  • Jurgen Willis

    Hi
    In the FizzVaders thing (there's a link to the source in another thread) I do the following...
    This goes in the class declaration

    static public ArrayList aliens = new ArrayList();

    Then when you add a new alien do...

    Alien a = new Alien(sprite, position, col);

    aliens.Add(a);

    Then to loop through them to, say, Update or Render do...

    foreach (Alien alien in aliens)

    {

    alien.Update(gameTime);

    }

    Hope that helps.


  • catalinione

    Heh, I want to read it already.

  • fabal

    For the timing, create a variable in the game class (say int _elapsedGameTime), set it to 0 in the Initialize event, and check in the Update event:

    _elapsedGameTime += gameTime.ElapsedGameTime.Seconds;

    if (_elapsedGameTime >= 10)
    {
        //spawn new ball


        //reset the variable
        _elapsedGameTime
    }



  • Newbie question - spawning new objects