3

重複の可能性:
XNA-ファイルが見つからない問題

ここでは、WindowsPhone7アプリケーションプロジェクトにRound.pngファイルを読み込もうとしています。実行時にこのイメージをロードする方法がわかりません。これがWindowsアプリ開発の初心者としてのばかげた質問である場合は本当に申し訳ありません。助けてください...よろしくお願いします!!!

     /// <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);
    }
}

エラーの詳細

これはエラーメッセージです

4

2 に答える 2

1

Round.pngファイルをContentディレクトリ(またはプロジェクト)に正しく配置し、デフォルト設定に従って正しくコンパイルされていることを前提としています。

ファイルを右クリックして、そのプロパティを開きます。「名前」フィールドをチェックします。それContent.Load<>が期待されていることです。したがって、たとえば、「Circular.png」というファイルをContentフォルダーにドラッグすると、「Circular」という名前が自動的に付けられますが、ファイルの名前を「Round.png」に変更すると、通常は「」という名前のままになります。循環」、プロパティの名前を手動で変更し、コード内のすべての参照を更新する必要があります。

于 2012-10-04T16:10:02.263 に答える
1

内部例外のパスは正しいように見えます:\"Content\\Round.xnb\"、余分なスラッシュは、エスケープされているからです。これは実行可能ファイルからの相対パスであり、デフォルトでは次のようになりますVisual Studio Projects\MyCoolGame\MyCoolGame\bin\x86\Debug\Content\Round.xnb。このパスをRound.xnb、pngではなくファイルへの実際のパスと比較する必要があります。

  • どこにも存在しない場合Round.xnbは、pngファイルがコンテンツプロジェクトの一部ではないか、コンテンツプロジェクトがビルドされていません
  • ファイルが存在するがサブディレクトリにある場合は、次のようにテクスチャをロードするときにサブディレクトリの名前を指定します。Content.Load<Texture2D>("myDir\\Round");

内部例外は「Round.png.xnb」を検索していないため、名前は正しく設定されており、@Ricketの回答はそれ以上役に立ちません。

于 2012-10-07T17:18:41.827 に答える