3

Possible Duplicate:
XNA - File not found problem

Here I am trying to load a Round.png file in windows phone 7 application project. I don't know how to load this image during run time. I am really sorry if this is a silly question as i m a newbie in windows app development. Please help... Thanks in advance!!!

     /// <summary>
     /// This is the main type for your game
     /// </summary>
   public class Game1 : Microsoft.Xna.Framework.Game
   {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D texRound;
    Rectangle HitRegion;
    bool isSelected = false;

    TouchCollection touches = TouchPanel.GetState();

    //start position of round, in the center of screen
    int positionX = 400;
    int positionY = 240;

    //random number Axis X and Y
    Random randomX;
    Random randomY;

    //the range for random number of start and end of X, Y
    int startX, endX;
    int startY, endY;

    //total time
    float milliseconds = 0f;

    //score count
    int count = 0;

    //game font
    SpriteFont font;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        // Frame rate is 30 fps by default for Windows Phone.
        TargetElapsedTime = TimeSpan.FromTicks(333333);

        // Extend battery life under lock.
        InactiveSleepTime = TimeSpan.FromSeconds(1);
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to       run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>

     protected override void Initialize()
     {
        // TODO: Add your initialization logic here

        base.Initialize();
     }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.

        spriteBatch = new SpriteBatch(GraphicsDevice);
        texRound = Content.Load<Texture2D>("Round");
        randomX = new Random();
        randomY = new Random();

        // The X axis bound range of touch for ball
        startX = texRound.Width;
        endX = GraphicsDevice.Viewport.Width - texRound.Width;

        // The X axis bound range of touch for ball
        startY = texRound.Height;
        endY = GraphicsDevice.Viewport.Height - texRound.Height;

        // Define the HitRegion of ball in the middle of touchscreen
        HitRegion = new Rectangle(positionX - texRound.Width / 2,
        positionY - texRound.Height / 2, texRound.Width,
        texRound.Height);

        // Load the font definition file
        font = Content.Load<SpriteFont>("gamefont");

        // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        // Accumulate the elapsed milliseconds every frame
        milliseconds +=
        (float)gameTime.ElapsedGameTime.TotalMilliseconds;
        if (milliseconds > 1000)
        {
            // When the milliseconds greater than 1000 milliseconds,
            // randomly locate a new position for the ball
            HitRegion.X = randomX.Next(startX, endX + 1);
            HitRegion.Y = randomY.Next(startY, endY + 1);
            // Reset the milliseconds to zero for new milliseconds
            // count
            // make the ball not been selected
            milliseconds = 0f;
            if (isSelected)
                isSelected = false;
        }
        base.Update(gameTime);

        Point touchPoint = new Point((int)touches[0].Position.X, (int)touches[0].Position.Y);
        if (HitRegion.Contains(touchPoint))
        {
            isSelected = true;
            count++;
        }
        else
        {
            isSelected = false;
        }
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Green);

        // TODO: Add your drawing code here

        spriteBatch.Begin();
        if (isSelected)
        {
            spriteBatch.Draw(texRound, HitRegion, Color.Red);
        }
        else
        {
            spriteBatch.Draw(texRound, HitRegion, Color.White);
        }
        spriteBatch.DrawString(font, "Score:" + count.ToString(),
        new Vector2(0f, 0f), Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

Error Details

this is the error message

Community
  • 1
  • 1
Sam
  • 925
  • 1
  • 12
  • 28
  • I did see it, I have added the content reference, but it doesn't help... – Sam Oct 04 '12 at 15:55
  • You've followed all of the debugging steps from [George's answer](http://stackoverflow.com/a/4648339/536974)? If so, please provide the full error message you get. – John McDonald Oct 04 '12 at 16:08
  • Error details please? (Click: View Detail...) I suspect it's looking for in the wrong directory. – John McDonald Oct 04 '12 at 17:01
  • yes i have followed all the steps... – Sam Oct 04 '12 at 17:01
  • If you click around in your exception, and inner exceptions, checking that the real paths and ones XNA is checking match up, you should find the issue pretty quickly. – John McDonald Oct 04 '12 at 17:24
  • first of all I m very sorry for late reply... yes, both paths are different but I don't know how to correct them.. – Sam Oct 07 '12 at 08:49

2 Answers2

1

I'm assuming you correctly put the Round.png file in your Content directory (or project) and it's being compiled properly, as per default settings.

Right-click the file and open its properties. Check the "Name" field. That's what Content.Load<> is expecting. So for example, if you drag a file called "Circular.png" into the Content folder, then it will be auto-named "Circular", but if you rename the file to "Round.png", typically it will remain named "Circular", leaving you to manually change the name in the properties and update all references in your code.

Ricket
  • 33,368
  • 30
  • 112
  • 143
  • The name field shows Round.png and I also tried by giving its complete physical path but nothing goes well.. – Sam Oct 04 '12 at 16:44
  • 1
    As I explained, you must type exactly the contents of the Name field into the string that you pass to `Content.Load<>`. So you need to use `Content.Load("Round.png")` then. Typically (and by default), the name is just the name part of the filename, minus the extension. So your other alternative is to fix the Name field to just say "Round", and then you should no longer see this error. – Ricket Oct 04 '12 at 16:57
  • I did that also but nothing happens...what I figure out was that the image should be loaded into the project before loading the project to emulator...But don't know if I am right or not. – Sam Oct 07 '12 at 08:53
  • Your project has a Content folder, right? Is the image in the Content folder? – Ricket Oct 07 '12 at 16:04
  • yep, it's in the same folder. – Sam Oct 09 '12 at 18:48
1

The path in the inner exception looks correct: \"Content\\Round.xnb\", the extra slashes are just because it's escaped. This is a relative path from your executable, be default it's something like: Visual Studio Projects\MyCoolGame\MyCoolGame\bin\x86\Debug\Content\Round.xnb. You should be comparing this path to the the real path to the Round.xnb file, not the png.

  • If Round.xnb does not exist anywhere, then your png file isn't part of Content project, or the Content project isn't being built
  • If the file does exist, but is in a subdirectory, specify the name of the subdirectory when you load the texture like: Content.Load<Texture2D>("myDir\\Round");

Since the inner exception isn't looking for "Round.png.xnb", you have your names up correctly and @Ricket's answer won't help you any further.

John McDonald
  • 1,790
  • 13
  • 20