DrawableGameComponent 是用于创建按钮的错误工具吗? [英] Is it DrawableGameComponent the wrong tool for creating buttons?

查看:15
本文介绍了DrawableGameComponent 是用于创建按钮的错误工具吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作基于游戏状态管理的游戏图案.我在第二页上做了 20 个按钮,让用户选择级别.

I am making a game based on Game State Managment pattern. I made 20 button on a 2nd page that let the user pick the level.

 LevelButton : TexturedDrawableGameComponent

在 LoadContent() 内部创建按钮并将其添加到 GameComponent 集合中.如果我做对了,GameCompoenent 是一种全局集合,因此当用户从第 4 页返回到第 2 个应用程序时,将添加另外 20 个按钮.

Inside LoadContent() buttons are created and added to GameComponent collection. If I get this right the GameCompoenent is a sort of global collection, so when the user goes back from 4th page to 2nd app will add another 20 buttons.

我添加了一个循环来清除集合中的所有 LevelButton,但似乎这是错误的.

I added a loop that clears all LevelButtons from the collection, but seems this is wrong.

DrawableGameComponent 是否是用于创建按钮的错误工具?

推荐答案

.

DrawableGameComponent 和它的朋友是 XNA 框架的可选位.将他们视为帮手.简单的示例实现.

DrawableGameComponent and its friends are optional bits of the XNA framework. Consider them helpers. Simple example implementations.

如果您需要更多或与其提供的功能不同的功能,您必须用您自己的实现替换它们!

If you need more or different functionality to what they provide, you must replace them with your own implementation!

在这种情况下,似乎您想要的是一个按钮"对象(例如您的 LevelButton),可能带有诸如 UpdateDraw.

In this case it seems that what you want is a "Button" object (like your LevelButton), probably with methods like Update and Draw.

然后你需要某种按钮容器"对象来保存所有这些按钮,并在它们出现在屏幕上时保持它们的绘制和更新.但不要在他们离开屏幕时对他们做任何事情.

And then you want some kind of "Button Container" object that will hold all these buttons, and keep them drawn and updated when they are on-screen. But not do anything with them while they are off-screen.

(您可以创建自己的 GameComponentCollection 实例来管理它,并继续使用游戏组件.但您仍然必须提供自己的绘制/更新/加载/卸载/初始化逻辑.逻辑Game.ComponentsGame 内部的,不可重用.我建议只使用 List - 保持简单!)

(You could create your own instance of GameComponentCollection to manage this, and keep using game components. But you still have to provide your own draw/update/load/unload/initialise logic. The logic for Game.Components is internal to Game and not reusable. I recommend just using a List - keep it simple!)

创建多个集合的替代方法 - 即从全局集合中添加和删除(或禁用/启用)组件 - 非常丑陋且容易出错.不要这样做.

The alternative to creating multiple collections - which is to add and remove (or disable/enable) components from the global collection - is horrifically ugly and error-prone. Don't do it.

这篇关于DrawableGameComponent 是用于创建按钮的错误工具吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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