2012-10-30 47 views
1

我在xna begginer,我可以加載一個圖像,但它不工作,拋出一個異常,沒有找到文件,我已經嘗試了一切衝浪每一個地方,很多人問,疑問句,無法找到答案,請幫忙,這裏的IM在很大的麻煩是代碼無法加載圖像在xna

protected override void Initialize() 
    { 

     base.Initialize(); 
    } 

    /// <summary> 
    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     mytexture = Content.Load<Texture2D>("gray"); 
     myrectangle = new Rectangle(100, 100, 40, 40); 

     // 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 

     base.Update(gameTime); 
    } 

    /// <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.CornflowerBlue); 

     // TODO: Add your drawing code here 
     spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
     spriteBatch.Draw(mytexture, myrectangle , Color.White); 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 

圖像中添加內容

+1

你需要提供更多的細節:拋出什麼異常,哪些文件找不到?哪個函數是錯誤的? – Barmar

+0

您是否已將「AssetName」「gray」圖片添加到您的內容項目中? –

+0

是內容加載器集的根文件夾? – Markus

回答

0

問題要麼該文件已被刪除或移動,並且不再能夠通過您的內容項目找到。

查看您的內容項目下的圖像列表。你很可能沒有在根中叫灰色的。如果有,有一些事情你可以檢查:

  1. 如果它是一個文件夾中,你需要與你的 Content.Load匹配起來(@「FOLDERNAME /灰色」)
  2. 確保它全部小寫,就像您的Content.Load指令
  3. 確保文件存在。如果它在那裏列出,請嘗試打開它。
+0

它被列在內容中,當點擊時它會在圖形設計器類型頁面中打開 – mani

+0

它不在文件夾中並且名稱完美匹配 – mani

0

感謝每個人,我發現解決方案,它是一個很奇怪的一個,我只是做一個新的項目,拷貝過來的一切,我改變什麼,但一個作品一個都沒有,反正非常感謝