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

How do I display a sprite?

Last post 6/4/2009 8:17 PM by dadoo Games. 7 replies.
  • 6/3/2009 2:15 PM

    How do I display a sprite?

    I'm puzzled.
    the msdn documentation looks fairly simple and the demo works well.
    But when I try it with my own file, I get an error that the resource wasn't found.

    The demo uses an .xnb file located in the "Content" directory.
    For my own game I also have a .png image located in the Content folder (in both cases the content's root directory is that exact folder), but regardless if I simply pass a string of the file name (with or without extension) or the absolute path of the file, I always get the same error:
    Error Message:

    Error loading "testing.png". File not found.


    Can I only load .xnb files?
    If so, how can I easily convert png/bmp images to that format?
    If not, what went wrong and how can I display that image?

    Thanks.
  • 6/3/2009 2:31 PM In reply to

    Re: How do I display a sprite?

    XNA will compile your image assets to .xnb files

    in your code you will just have to use the contentmanager and the path to your filename without extension, because the generic cast will determine the kind of asset you are trying to load, in the example below i am using your testing.png as an Texture2D object.

    ContentManager content = new ContentManager(ScreenManager.Game.Services, "Content");
    Texture2D test= content.Load<Texture2D>("testing");

    And in your draw method you can do for example the following:

    spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
    spriteBatch.Draw(this.test, new Vector2(0,0), Color.White);
    spriteBatch.End();


    Hope this helps.
  • 6/3/2009 2:34 PM In reply to

    Re: How do I display a sprite?

    Sadly, this does not help.
    This is exactly what I've been doing so far.
  • 6/3/2009 2:42 PM In reply to

    Re: How do I display a sprite?

    Could you maybe post your code, then I/We can have a closer look at the problem?
    cause this would just work in a normal situation...

    Your testing.png is in the Content folder and included in your project (so it will be compiled to .xnb)?

  • 6/3/2009 2:45 PM In reply to

    Re: How do I display a sprite?

    Might help to look at my step by step tutorials on "Adding an Image to the Game Project" and on "Creating a 2D sprite". They might help you spot the step you're skipping in loading and displaying your own image.
  • 6/4/2009 8:56 AM In reply to

    Re: How do I display a sprite?

    Thanks, that was the solution.
    I assumed it works like displaying a Bitmap, just passing the location alone.
    But you also have to add it to the project's content.
  • 6/4/2009 8:13 PM In reply to

    Re: How do I display a sprite?

    you can also use:

    Texture2D texture = Texture2D.fromFile(graphicsDevice, "Sprites\\my_sprite.png");

    to load sprites from *.png file itself so when you would want to change your image all you have to do is replace the image file.

  • 6/4/2009 8:17 PM In reply to

    Re: How do I display a sprite?

    Texture2D.FromFile() won't work on the Xbox. If you're going to repeatedly suggest it you might want to add that disclaimer.
Page 1 of 1 (8 items) Previous Next