2015-08-26 64 views
0

我剛開始使用Monogame,我正在嘗試製作一個簡單的雪碧,後來就是一個按鈕。我搜索了所有內容並完成了幾個教程,但是我無法完成它。我只是不斷得到空白的藍屏。這裏是我的代碼:使用Monogame繪製雪碧

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Content; 

namespace Test_Game 
{ 

    class Main_Menu 
    { 
     //setting the variables 
     public Texture2D button1; 
     public Vector2 button1Pos; 
     public GraphicsDevice graphicsDevice; 
     GraphicsDeviceManager graphics; 

     public void initialize(Texture2D texture, Vector2 position, ContentManager Content) 
     { 
      //Getting the initialized stuff 
      button1 = Content.Load<Texture2D>("button_temp"); 
      button1Pos.X = 30; 
      button1Pos.Y = 30; 
     } 

     public void Draw(SpriteBatch spriteBatch) 
     { 
      graphics.GraphicsDevice.Clear(Color.Black); 
      spriteBatch = new SpriteBatch(graphicsDevice); 
      //Just drawing the Sprite 
      spriteBatch.Begin(); 
      spriteBatch.Draw(button1, new Rectangle(30, 30, 214, 101), Color.White); 
      spriteBatch.End(); 
     } 
    } 
} 

希望你能找到答案。

+0

空白藍屏 - 然後在您的畫圖中將其設置爲黑色。你是否從你的主要「game1」類中調用了你的繪製方法? – harag

回答

1

我可以在你的代碼中看到許多錯誤,我會留下一條評論指出所有的錯誤,但它太長了。

大部分的錯誤都來自這個:你不是從Game類繼承。您的線路class Main_Menu應該是class Main_Menu : Game。始終使用this template對於遊戲類:

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace MyGame 
{ 
    public class MyGame : Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 

     public MyGame() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 
     protected override void Initialize() 
     { 
      base.Initialize(); 
     } 
     protected override void LoadContent() 
     { 
      spriteBatch = new SpriteBatch(GraphicsDevice); 
     } 
     protected override void UnloadContent() 
     { 
     } 
     protected override void Update(GameTime gameTime) 
     { 
      base.Update(gameTime); 
     } 
     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 
      base.Draw(gameTime); 
     } 
    } 
} 

從這裏開始,你必須填寫此模板注意以下事項:

  1. 創建你的記憶,只在Initialize方法對象;
  2. LoadContent方法中加載並創建與文件相關的對象;
  3. Update方法中添加您的遊戲邏輯;
  4. Draw方法中添加繪圖邏輯;
  5. 通常,不要打擾構造函數或UnloadContent方法。

與模板連接現有的代碼,我們得到如下:

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace Test_Game 
{ 
    public class Main_Menu : Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     Vector2 buttonPos; // our button position 
     Texture2D button; // our button texture 

     public MyGame() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 
     protected override void Initialize() 
     { 
      buttonPos = new Vector2(30, 30); // X=30, Y=30 
      base.Initialize(); 
     } 
     protected override void LoadContent() 
     { 
      spriteBatch = new SpriteBatch(GraphicsDevice); 
      button = Content.Load<Texture2D>("button_temp"); // load texture 
     } 
     protected override void UnloadContent() 
     { 
     } 
     protected override void Update(GameTime gameTime) 
     { 
      // here we would add game logic 
      // things like moving game objects, detecting collisions, etc 
      base.Update(gameTime); 
     } 
     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 
      spriteBatch.Begin(); 
      // draw our button 
      int buttonWidth = 214; 
      int buttonHeight = 101; 
      spriteBatch.Draw(button, new Rectangle(buttonPos.X, buttonPos.Y, buttonWidth, buttonHeight), Color.White); 
      spriteBatch.End(); 
      base.Draw(gameTime); 
     } 
    } 
} 

這個模板在每場比賽的MonoGame和XNA框架中使用,所以你應該找到的內容在很多關於Game類的每種方法的功能。