XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

SpriteBatch.Draw - Looping through an array of textures, some are not being drawn.

Last post 03/10/2007 19:43 by The Thing. 5 replies.
  • 03/10/2007 18:34

    SpriteBatch.Draw - Looping through an array of textures, some are not being drawn.

    Hi folks,

    I am working on a demo - nothing too complicated - which I am planning to release back to the XNA community at some stage in the hope that some XNA newbies like myself might learn something from it.

    I've broken my demo down into several classes:

    1 - A DemoManager class which is the main demo class.
    2 - A spriteManager class to handle my textures.
    3 - A DemoSettings class to handle the demo variables like the number of sprites to draw, screen size, etc.
    4 - A userInputManager class to handle user input (this hasn't been implemeted yet)

    In my DemoManager class I have created an array of SpriteManager objects and when I iterate through the array in the DemoManager's Draw() method most of the time only some of the sprites will be drawn. For example if I set the number of sprites to be drawn to 15 in the DemoSettings class I might be lucky if I get 10 drawn to the screen.    

    //******************************************
    //Edited By The Thing - 5.10.2007 - Removed download link
    //******************************************

    I'd be grateful if somebody here would take a look at it for me and let me know what the problem is.

    Thank you.
     




    Man Is The Warmest Place To Hide
  • 03/10/2007 18:51 In reply to

    Re: SpriteBatch.Draw - Looping through an array of textures, some are not being drawn.

    All I had to do was change your draw loop to using the SpriteManager draw method instead of manually calling the sprite batch and it worked fine.

    That said, I would hesitate to release this yet. There are lots of things you could improve on. The first is the fact that your main game stores all the data for a sprite before creating it. There is no need to have your game hang on to that data. You should simply create it, pass it to the sprite, and then be done with it. That could be done using local variables instead of class-level variables.

    Another thing you would want to avoid is actually calling new SpriteManager in the LoadGraphicsContent method. You also probably wouldn't want to be initializing them there either. If the graphics device gets recreated, all your sprites will revert back to their original states and you will lose all information about where they are. I'd suggest creating them and initializing them in your game's initialize method and then simply loading the sprite batch and textures in LoadGraphicsContent.

    A more minor note, I would rename the SpriteManager class to just Sprite. Naming it SpriteManager gives the impression that one instance handles multiple sprites which isn't the case. Just a minor thing that tripped me up when I first looked at the code.

    Otherwise it's not a bad start. Keep at it and I'm sure it'll turn out to be a big help to the community once it's ready.
  • 03/10/2007 18:53 In reply to

    Re: SpriteBatch.Draw - Looping through an array of textures, some are not being drawn.

    Hm. Running it a few more times, it isn't consistent. I think the problem is that some sprites are being placed on other sprites, but I'll see if I can find out for sure.
  • 03/10/2007 18:56 In reply to

    Re: SpriteBatch.Draw - Looping through an array of textures, some are not being drawn.

    One more very big thing. Your SpriteManager class inherits the XNA Game class. That should be a very big no-no. There is no need for the SpriteManager class to inherit anything in this particular example. If you were going to use them as GameComponents you could inherit that, but there is no need to inherit from Game.

    (By the way, I hope I don't sound mean or anything, I'm trying my best to just make constructive criticism to help you out. Don't take it the wrong way.)
  • 03/10/2007 19:08 In reply to

    Re: SpriteBatch.Draw - Looping through an array of textures, some are not being drawn.

    Alrighty. I did a little refactoring, renaming, and cleaning up. Here's what the class looks like if you take my suggestions. Also, this seems to fix up the problem of your objects drawing incorrectly. My guess is that it has to do with the Random number generator being seeded the same values somehow when you were creating it, but I'm really not sure. This seems to work though.

    Either way, take this as an idea of how to set things up: http://xna.multigan.com/pastebin/?page=view&id=1191452566.
  • 03/10/2007 19:43 In reply to

    Re: SpriteBatch.Draw - Looping through an array of textures, some are not being drawn.

    Hi Nick, thanks for the prompt replies.

    The only reason I uploaded it in it's current form was to allow someone such as yourself the chance to run it in order to figure out what was wrong with it - I know that as it stands uncorrected it is not ready for release, that's why the code has no comments for one thing.

    I've been messing around with it for a few days now trying to figure out the problem and I've been chopping and changing code to see what happens - I did realise afterwards that my SpriteManager class (soon to be renamed to something more appropriate :) )  had no business inheriting from Game - again I just tried that to see what happens and forgot to remove it.

    Some of the tutorials aimed at beginners tend to lump everything into one class (Game1 : Microsoft.Xna.Framework.Game) and as such it's often a bit confusing as to how one would go about breaking up functionality into seperate classes. Perhaps a tutorial would be of benefit to community members such as myself.

    Apart from all that I do appreciate your input and I understood exactly where you were coming from in your criticisms of the code I uploaded. I think the fact that you thought you were coming across as being mean shows that you care about what you do. Thanks for the refactored code, I will incorporate your changes as soon as possible.

    All the best for now.

    The Thing.
     

        



         

         
         
    Man Is The Warmest Place To Hide
Page 1 of 1 (6 items) Previous Next