2015-08-26 63 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类的每种方法的功能。