none
Problem whit gameTime class...

    Question

  • Hi, i'm new to xna and c#... I don't know how to create sprites automatically and randomly  because trying this:

    GameTime gameTime; 
    Random random = new Random(); 
    Texture2D sprite; 
    Vector2 position; 
    int minEnemySpawn = 300; 
    int maxEnemySpawn = 1000; 
    int nextSpawn = 0; 
     
    public void Update(GameTime gameTime) 
            { 
     
                nextSpawn += (int)gameTime.ElapsedGameTime.TotalMilliseconds; 
                 
     
                    if (nextSpawn < 0) 
                    { 
                        spawnEnemy(contentManager); 
                    } 
            }
    private void resetTimer() 
            { 
                Random n = new Random((int)DateTime.Now.Ticks); 
                nextSpawn = n.Next(minEnemySpawn, maxEnemySpawn); 
            } 
     
            private void spawnEnemy(ContentManager contentManager) 
            { 
                position = new Vector2(random.Next(80, 400), 0); 
     
                if (nextSpawn <= 650) 
                { 
                    sprite= contentManager.Load<Texture2D>("..."); 
                } 
     
                else 
                { 
                    sprite = contentManager.Load<Texture2D>("..."); 
                } 
     
                resetTimer(); 
            } 

    When I debug comes out this error: NullReferenceException for "nextSpawn += (int)gameTime.ElapsedGameTime.TotalMilliseconds;" because the gameTime is null. 
     I don't know what to do.
    Please help me, thank you!
    Saturday, November 19, 2011 7:50 PM

Answers

  • finally I resolve it
    for solve i use this code: 
    private void OnUpdate(object sender, GameTimerEventArgs e) 
            { 
     
                TimeSpan elapsedGameTime = e.ElapsedTime; 
                nextSpawn -= (int)e.ElapsedTime.Milliseconds; 
                tempo += e.ElapsedTime.Milliseconds;  
            } 


    Saturday, November 26, 2011 12:13 PM

All replies

  • Sorry for the double post but I don't really know what to do because I don't know why the gameTime is set on null... How can I change the set of gameTime?
    Sunday, November 20, 2011 1:50 PM
  • Hello There!

    From only what you have shown above, it looks like you never initialize the gameTime variable! I think what is needed here to help us help you is some more of your code, specifically where and how you are calling the update method!

    It also looks you are not using a GameComponent to handle this update, in which XNA will take care of providing you with the current gameTime variable, so that might also be something you might want to look in to!


    Hope this helps!

    Sincerely,

    Onewaysidewalks
    Sunday, November 20, 2011 6:26 PM
  • maybe is because i put the code in my Sprite class and not in the game class?

    I don't have the code because i'm in an other computer so i can't try the code... but i hope is right if not I don't know what to do...
    Wednesday, November 23, 2011 1:19 PM
  • If you are using the XNA game framework the GameTime is passed to the update class for you as part of the XNA game loop.
    All you need to do is call the Sprite.Update(gameTime) from the main Game.Update(GameTime gameTime) passing that gameTime to your sprite update method.

    // GAME Class  
     
    public void Update(GameTime gameTime)  
    {  
        // Your stuff here  
        YourSpriteClass.Update(gameTime);  
     
        base.Update(gameTime);  

    Something like that.

    Regards

    Wednesday, November 23, 2011 2:21 PM
  • I'm using an Xna/Silverlight app and I've tried to do what you say but I always receive that the gameTime and the contentManager are 'null'
    Wednesday, November 23, 2011 6:00 PM
  • It would really help if you would post the method (at least) in which you call your update! That way we can have a better look at what is going wrong!

    Let us know!
    Friday, November 25, 2011 8:06 PM
  • finally I resolve it
    for solve i use this code: 
    private void OnUpdate(object sender, GameTimerEventArgs e) 
            { 
     
                TimeSpan elapsedGameTime = e.ElapsedTime; 
                nextSpawn -= (int)e.ElapsedTime.Milliseconds; 
                tempo += e.ElapsedTime.Milliseconds;  
            } 


    Saturday, November 26, 2011 12:13 PM