简体   繁体   English

使用c#和xna在单独的类中加载texture2D

[英]Loading a texture2D in a separate class using c# and xna

I have been adding a bunch of textures to my game in xna so i decided to make a separate class to load and draw the textures. 我已经在xna的游戏中添加了一堆纹理,所以我决定创建一个单独的类来加载和绘制纹理。

this is what i have so far 这就是我到目前为止

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);
    }
}
}

the problem is that it can't find the button texture and i know it isn't spelt wrong as when i load it in the Load method of my game it works 问题是它找不到按钮纹理,而且我知道拼写没有错,因为我在游戏的Load方法中加载它时可以正常工作

Whenever you create a new XNA project there is this inside the constructor : 每当您创建新的XNA项目时,构造函数中都将包含以下内容:

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

Alternatively you could do the following : 另外,您可以执行以下操作:

    class Game
    {

    }

    class TextureManager
    {
        public TextureManager(Game game)
        {

        } 
    }

Note, https://gamedev.stackexchange.com/ will be a better place for you to ask game-dev related questions. 请注意, https://gamedev.stackexchange.com/将是您提出与游戏开发相关的问题的更好场所。

You can also call your load-method from The game1-loadcontent-void ans gibe content-Manager as a parameter. 您也可以从Game1-loadcontent-void ans gibe content-Manager调用加载方法作为参数。 Then you can call Content.Loaf in The other class as well. 然后,您也可以在另一个类中调用Content.Loaf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM