开发者

XNA GraphicsDevice cannot be called from other classes

everyone! I'm developing a platformer in XNA/C# and I'm running into problems wi开发者_开发百科th GraphicsDevices. I've heard that XNA requires GraphicsDevices in a lot of methods, but it can only be called after LoadContent() which is problematic for me, as I'm going to have a lot of classes in my project. How can I get the GraphicsDevice?

Thanks in advance!

EDIT: Here's a code example: http://pastebin.com/vrYhA6MA


In your Game class, add a GraphicsDeviceManager variable like so:

GraphicsDeviceManager graphics;

Now, in your Game constructor, initialize it like so:

graphics = new GraphicsDeviceManager(this);

After the Game constructor, within both the Initialize and the LoadContent methods, the graphics.GraphicsDevice variable should be loaded. You can pass it into methods and give it settings, like DepthStencilState, etc.


It has always been my prefered method to pass a reference to the Game object in through the constructors of any other classes. That way I can call

this.Texture = game.Content.Load<Texture2d>("Fred");

For example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜