【问题标题】:Loading a texture2D in a separate class using c# and xna使用 c# 和 xna 在单独的类中加载纹理 2D
【发布时间】:2013-04-08 02:01:59
【问题描述】:

我在 xna 中为我的游戏添加了一堆纹理,所以我决定创建一个单独的类来加载和绘制纹理。

这是我目前所拥有的

using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Trolls_and_Towers
{

class Textures
{
    //Texture loading

        //Buttons
    public static Texture2D button;


    public static void Load()
    {
        Microsoft.Xna.Framework.Game game = new Microsoft.Xna.Framework.Game();
        button = game.Content.Load<Texture2D>("button");
    }

    public static void Draw()
    {
        Game1.spriteBatch.Draw(button, new Rectangle(20, Game1.screenHeight - 70, 100, 50), Color.White);
    }
}
}

问题是它找不到按钮纹理,我知道它没有拼写错误,因为当我在游戏的 Load 方法中加载它时它可以工作

【问题讨论】:

    标签: c# xna texture2d


    【解决方案1】:

    每当你创建一个新的 XNA 项目时,构造函数中都会有这个:

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
    

    或者,您可以执行以下操作:

        class Game
        {
    
        }
    
        class TextureManager
        {
            public TextureManager(Game game)
            {
    
            } 
        }
    

    请注意,https://gamedev.stackexchange.com/ 将是您提出游戏开发相关问题的更好场所。

    【讨论】:

      【解决方案2】:

      您还可以从 game1-loadcontent-void 和 gibe content-Manager 中调用您的加载方法作为参数。然后你也可以在另一个类中调用 Content.Loaf。

      【讨论】:

        猜你喜欢
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多