【问题标题】:Drawing a Sprite using Monogame使用 Monogame 绘制 Sprite
【发布时间】:2015-08-26 19:52:58
【问题描述】:

我刚开始使用 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();
        }
    }
}

希望你能找到答案。

【问题讨论】:

  • 空白蓝屏 - 但在您的绘图中,您将其设置为黑色。你是从你的主“game1”类中调用你的draw方法吗?

标签: c# sprite monogame


【解决方案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 类的每个方法的作用的内容。

【讨论】:

    猜你喜欢
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多