制作动态场景管理器?

发布于 2024-12-25 02:57:44 字数 344 浏览 5 评论 0原文

现在,我的场景管理器的工作方式是使用枚举和开关盒。

我有一个场景可以调用的方法,称为changeScene(MENU_SCENE) 等等。

在场景管理器本身中,我有一个 switch case,上面写着 case MENU_SCENE: return new MenuScene()。

这意味着每次添加新场景时,我都必须将其添加到 switch case 中。

有没有更好的方法可以在没有开关盒的情况下做到这一点?理想情况下,我想要给定一个字符串,返回一个 BaseScene* ,但我能想到的方式仍然会涉及 switch case (或嵌套 if ,因为不允许 case 中的字符串)。

谢谢

Right now, the way my scene manager works is with an enum and a switch case.

I have a method that a scene can call called changeScene(MENU_SCENE) and so on.

In the scene manager itself, I have a switch case that says, case MENU_SCENE: return new MenuScene().

This means each time I add a new scene I must add it to the switch case.

Is there a batter way to do this without a switch case? Ideally I'd like something where given a string, returns a BaseScene*, but the way I can think of it would still involve a switch case (or nested ifs since strings in cases are not permitted).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2025-01-01 02:57:44

似乎您想要一个以字符串为键的地图。该值取决于现在 switch 语句中的内容。它可以像下一个字符串一样简单,也可以是指向要调用的函数的指针(如果您需要针对每种情况使用特殊代码)。

Seems like you want a map where the string is the key. The value depends on what's in your switch statement now. It could be as simple as the next string, or it could be a pointer to a function to call if you need special code for each case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文