使用Content.Load<>在类/子类中 [英] Using Content.Load<> in a class/subclass

查看:103
本文介绍了使用Content.Load<>在类/子类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否还有在不是游戏本身的类中使用Content.Load<>,例如我想从类中加载纹理,而不是发送纹理

I'm wondering if there is anyway to use the "Content.Load<>" in a class that is not the game itself, say I want to load a texture from within the class, rather than sending the texture to it.

namespace ProjectGame1
{
    public class Ship
    {
        public Texture2D texture;

        public Ship()
        {
        this.texture = Content.Load<Texture2D>("ship");
        }
    }
}

推荐答案

您只需要将ContentManager传递到Ship对象:

You just need to pass your ContentManager to the Ship object:

public Ship(ContentManager content)
{
   this.texture = content.Load<Texture2D>("ship");
}

在您的游戏类中,您可以实例化Ship:

From your game class you instantiate the Ship:

Ship ship = new Ship(this.Content);

这篇关于使用Content.Load&lt;&gt;在类/子类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆