制作动态场景管理器?
现在,我的场景管理器的工作方式是使用枚举和开关盒。
我有一个场景可以调用的方法,称为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您想要一个以字符串为键的地图。该值取决于现在 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.