Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Okay, I'm writing a game in C#/XNA 4.0 and am utterly stumped at my current juncture: Sprite animation. I understand how it works and have all the code in place, but my ContentLoader won't open my file...

Basically, my directory looks like this:

    //WindowsGame1
      - "Game1.cs"
      - //Classes
          - "NPC.cs"
      - Content Reference
          - //Images
              - "Monster.png"

Inside my NPC class, I have all the essential drawing functions, i.e. LoadContent, Draw, Update. And I can get the game to find the correct file and attempt to open it, but when it tries, it throws an exception and tells me it can't open the file. This is how my code in my NPC class looks:

    Texture2D NPCImage;
    Vector2 NPCPosition;
    Animation NPCAnimation = new Animation();

    public void Initialize()
    {
        NPCAnimation.Initialize(NPCPosition, new Vector2(4, 4));
    }

    public void LoadContent(ContentManager Content)
    {
        NPCImage = Content.Load<Texture2D>("_InsertImageFilePathHere_");
        NPCAnimation.AnimationImage = NPCImage;
    }

The rest of the code is irrelevant at this point because I can't even get the image to load. I think it might have to do with a directory problem, but I also know little to nothing about spriting or working with images or animations in my code. Any help is appreciated. Not sure if I provided enough information here, so let me know if more is needed! Also, what would be the correct way to direct that Content.Load to Monster.png given the current directory situation? Right now I just have it using the full path from the C:// drive. Thanks in advance!

share|improve this question
1  
Actually what you insert in InsertImageFilePathHere is the path and assetname, in your case it would be "Images/Monster" because XNA would just trim the .png and use "Monster" as the default assetname –  Blu3 Jun 30 '13 at 11:33
    
Is that my problem though? –  user32623 Jun 30 '13 at 17:07
    
What does the exception actually say? –  Andrew Russell Jul 1 '13 at 4:36
    
An unhandled exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occurred in Microsoft.Xna.Framework.dll Additional information: Error loading "C:\Users\Donutdood\Documents\Visual Studio 2012\Projects\SwagDungeon\SwagDungeonStuff\Resources\Monsters\Boulder\Walking". Cannot open file. –  user32623 Jul 1 '13 at 16:01

1 Answer 1

Make sure you include the path to the image (relative to the content folder) and do not include ".png" XNA will convert the .png to a .xnb (XNA binary) as part of the content pipeline, so you don't want to reference the .png portion of the file in your code.

On rare occasions I've also seen people with problems because of their PNG was corrupt. If the above doesn't fix the problem, try another png file... something simple you find from a quick google search.

share|improve this answer
    
Okay, here's my question: Is it possible to make an animation from a collection of images, rather than a giant string of them one after another in the same image file? Because the walking animations I have for some of my creatures consist of at least 20 images, and side by side, that's over 3,900 pixels... –  user32623 Jul 1 '13 at 3:51
    
@user32623 It's best to ask a fresh question for something like that. But, yes, you can use separate image files if you like. You could also tile them in 2D, to stay below the maximum texture dimensions. –  Andrew Russell Jul 1 '13 at 4:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.