XNA游戏组件 [英] XNA game components

查看:133
本文介绍了XNA游戏组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何将变量值从一个游戏组件传递到另一个组件感到困惑. 我正在使用xna 4.0.

I an confused about how to pass values of variables from 1 gamecomponent to another. I am using xna 4.0.

我创建了两个游戏组件,拉绳和inputmanager. 我想读取用户的键盘输入,并将其传递到将更新位置的束线上.

I created two gamecomponents, the drawstring and the inputmanager. I want to read the keyboard input of the user and pass it onto drawstring where it will update the position.

我无法在drawstring(drawablegamecomponent)上添加组件. 我可以在课堂上做,但不能在gamecomponent上做. 你们能在这里发布一些例子吗?对于初学者.

I cant add components on drawstring(drawablegamecomponent). I can do it on class but not on gamecomponent. Can you guys post some examples here. For beginners.

推荐答案

对于要在每个帧上调用Update的内容,请使用GameComponent;对于您希望具有的内容,请使用DrawableGameComponent.在每个帧上调用Draw,并在适当的时候(在程序开始时以及设备丢失时,例如在用户按下 Ctrl-Alt-Del 时)调用LoadContent > (在Windows上).

Use GameComponent for something that you would want to have Update called on every frame, and use DrawableGameComponent for something that you would want to have Draw called on every frame and LoadContent called when appropriate (at the start of the program, as well as whenever the device is lost, like when the user pressed Ctrl-Alt-Del on Windows).

对于InputManager,您可能需要Update方法,以便可以使用户输入保持更新,因此InputManager可以是GameComponent. DrawString听起来好像不是必须的.这两个类听起来都像是服务.在游戏类的构造函数或Initialize方法中,执行以下操作:

For InputManager you might want an Update method, so that you can keep user input updated, so InputManager could be a GameComponent. DrawString doesn't sound like it needs to be. Both classes sound like they could be services. In the constructor or Initialize method of your game class, do something like the following:

Components.Add(mInputManager = new InputManager(this));
Services.AddService(typeof(InputManager), mInputManager);
Services.AddService(typeof(DrawString), mDrawString = new DrawString(this))

(DrawString和您要从中获取游戏服务的任何其他类都需要对Game对象的引用.)

(DrawString and any other class that you want to get game services from will need a reference to the Game object.)

(请注意,GameComponent不必一定是服务,并且服务不一定必须是GameComponent s.要调用Update和/或Draw,必须调用Components.Add(...);要使对象作为服务可检索,必须分别调用Services.AddService(...)).

(Note that GameComponents do not necessarily need to be services, and services do not need necessarily need to be GameComponents. To get Update and/or Draw to be called, you must call Components.Add(...); separately, to get an object to be retrievable as a service, you must call Services.AddService(...)).

然后,当您想在其他游戏组件(或传递了对游戏对象的引用的任何类)中使用InputManagerDrawString服务时,可以执行以下操作:

Then, when you would like to use the InputManager or DrawString service in some other game component (or any class that you have passed a reference to your game object), you can do this:

InputManager input = (InputManager)Game.Services.GetService(typeof(InputManager));

我个人编写了一个扩展方法以使其更简洁:

Personally, I write an extension method to make that more concise:

using XNAGame = Microsoft.XNA.Framework.Game;

...

   public static T GetService<T>(this XNAGame pXNAGame)
   {
      return (T)pXNAGame.Services.GetService(typeof(T));
   }

获取输入服务的行将变为:

The line to get the input service then becomes:

InputManager input = Game.GetService<InputManager>();

这篇关于XNA游戏组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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