处理跨多个类装载的质感和访问在XNA的最佳方式? [英] Best way to handle texture loading and access across multiple classes in XNA?

查看:194
本文介绍了处理跨多个类装载的质感和访问在XNA的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个简单的XNA项目正在进行。基本上我有问题是如何处理的纹理加载和确保有从其他类的纹理适当的访问权限?

So I have a simple XNA project going on. Basically the question I have is how to handle texture loading and making sure there is proper access to those textures from other classes?

例如,每个教程中,我所看到的,还有什么我可以从XNA的实际逻辑来讲,你应该加载在 LoadContent()方法纹理。但是,让我们说我有另一个类,等级,也需要有自己的一套独特的纹理,并在我创造我的播放器的对象,这也需要自己的质感,当然敌人和一切的。

For example, every tutorial I've seen, as well as what I can tell from the actual logic of XNA, you're supposed to load textures in the LoadContent() method. But let's say I have another class, Level, that needs its own unique set of textures, and within that I create an instance of my Player object, which also needs its own texture, and of course enemies and everything else.

我能做到这一点的方法之一是只加载所需的纹理在其构造特定的类,但我会需要建立一个新的内容管理器或通过内容管理到每个构造函数,它显得既笨拙,与原意之外。

One way I could do it is just to load the textures needed for a particular class in its constructor, but for that I'd need to set up a new content manager or pass the content manager to each constructor, which seems both unwieldy and outside of what was intended.

所以,当谈到那些不是使用2纹理的小教程全面的项目,什么是执行加载它们的最好方法是什么?

So when it comes to full-blown projects that aren't little tutorials that use 2 textures, what's the best way to implement loading them?

推荐答案

创建,因为你需要尽可能多的 ContentManager 类。

Create as many ContentManager classes as you need.

每是自包含的。在每个内容管理,资源(如:纹理)重复使用 - 但如果你加载在两个不同的内容管理者相同的纹理,你会得到两个不同的实例(尽量避免这种情况)

Each one is self-contained. Within each content manager, resources (eg: textures) are reused - but if you load the same texture in two different content managers, you get two different instances (try to avoid this).

您不能卸载(的Dispose())的个人资源,一个 ContentManager 负载。您只能卸载()的全部内容管理器(处置它装载的一切)。这可能因素到什么时候创建内容管理的任何决定。

You cannot unload (Dispose() of) individual resources that a ContentManager loads. You can only Unload() the entire content manager (disposing of everything that it loaded). This is likely to factor into any decision about when to create content managers.

您也许想创建一个 ContentManager 每级。再有另一个 ContentManager 处理事情,不需要级别之间进行卸载(如:东西你的播放器或敌人的对象需要)。

You perhaps want to create one ContentManager per level. And then have another ContentManager for handling things that don't need to be unloaded between levels (eg: the things your Player or enemy objects need).

有什么不对 ContentManager的传球实例各地,无论是。

There's nothing wrong with passing instances of ContentManager around, either.

当然,对于简单的游戏,它往往是最简单的只使用一个单一的 ContentManager ,而不必担心卸载的东西。

Of course, for simple games, it's often easiest to just use a single ContentManager and not worry about unloading things.

这篇关于处理跨多个类装载的质感和访问在XNA的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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