如何组织多个xna游戏组件?
我正在 XNA 中编程,需要帮助组织我的类和代码: 我有一个按钮类和一个按钮管理器。 我还有一个场景类和一个场景管理器。
按钮管理器处理将在不同屏幕上绘制的不同按钮,因为几乎所有屏幕都会有一个按钮。 场景管理器执行相同的操作,只不过它不处理按钮,而是处理只需要绘制的背景场景对象。 两个管理器都根据当前的游戏状态来确定要绘制哪些按钮或场景对象。
我应该如何组织我的代码,以便两位经理都知道当前的游戏状态是什么? (两个管理器都在主游戏类中实例化,并且两个管理器都是游戏组件)
I am programming in XNA and need help on organizing my classes and code:
I have a button class and a button manager.
I also have a scene class and a scene manager.
The button manager handles the different buttons that would be drawn on different screens since almost all screens would have a button.
The scene manager does the same thing except instead of handling buttons it handles background scene objects that just need to be drawn.
Both managers depend on the current game state to determine which buttons or scene objects to draw.
How should I organize my code so that both managers know what the current game state is? (Both managers are instantiated inside of the main game class and both managers are game components)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的描述您的问题的关键字是“游戏状态管理” XNA 网站上有一些关于它的好文章,请务必阅读此一篇:http://create.msdn.com/en-US/education/catalog/sample/game_state_management
现在更直接地回答您的问题。假设你有 2 个不同的状态
-菜单
-Game
首先创建一个名为 State 的类,其中包含设置绘制和更新正确 UI 元素的方法。
现在创建一个从 State 派生的类 MenuState 并重写 setup、draw 和 update 方法。在设置方法中放置所有代码以生成正确的菜单。 (如 Scene.MenuItems.Clear();Scene.MenuItems.Add(new Label(..));等等)。对更新和绘制方法执行相同的操作(更新和绘制每个控件、捕获通过单击按钮等给出的事件...)
对 GameState 执行相同的操作。
现在在您的场景代码中创建一个“State state”字段。当用户按下 escape 将状态设置为(新的)MenuState。当用户返回游戏时,将状态设置为(新的)GameState。在场景的 update 和 draw 方法中调用 state.Update(..) 和 state.Draw(..)。因为您已经重写了 GameState 和 MenuState 中的这些方法,所以将执行正确的操作。
这种方法解决了使用大量控件执行“if(scene.StateEnum == StateEnum.SomeState){DoThis();}”之类的检查的问题。您会发现这种方式更易于管理。
还要考虑构建其他概念类。就像 MenuState 可以有一个子状态“选项菜单”一样。也许想出一个表格类,等等......
The keyword you are looking for that describes your problem is "Game state management" The XNA website has a few good articles on it, be sure to read this one: http://create.msdn.com/en-US/education/catalog/sample/game_state_management
Now to answer your question more directly. Say you have 2 different states
-Menu
-Game
First create a class called State with methods to setup draw and update the correct UI elements.
Now create a class MenuState deriving from State and override the setup, draw and update methods. In the setup method put all the code to generate the correct menu. (Like Scene.MenuItems.Clear(); Scene.MenuItems.Add(new Label(..)); etc..). Do the same for the update and draw methods (update and draw each control, capture events given by clicks on buttons etc...)
Do the same for GameState.
Now in your Scene code make a field "State state". When a user presses escape set state to (a new) MenuState. When a user returns to game set state to (a new) GameState. In the scene's update and draw methods place a call to state.Update(..) and state.Draw(..). Because you've overriden these methods in GameState and MenuState the correct actions will be performed.
This approach solves the issue of having a gazzillion controls that do checks like "if(scene.StateEnum == StateEnum.SomeState){DoThis();}". You will find this way easier to manage.
Also think about building other conceptual classes. Like the MenuState could have a substate, "Options menu". Maybe think up a form class, etc...
由于 GameComponent 具有 Game 属性,因此您可以使用它来转换为 Game 类,或者获取公开游戏状态的服务。
SInce GameComponent has a Game property, you can use this to cast to the Game class, or alternatively to get a Service exposing the Game Status.