如何在Cocos2D中的多个场景中访问一个ivar?
感谢您百忙之中看我的问题! 在我的游戏中,我有几个场景。我有一个 IntroScene、一个 HomeScene 和一个 PlayingScene。现在,如果我需要创建一个变量 int playerStrength 并且我希望它从 0 开始,但它需要显示在我的 HomeScene 中,并且 PlayingScene 需要能够访问它嗯,我该怎么做呢?因为 HomeScene 需要显示这个整数,而 PlayingScene 需要知道它是什么,以便它可以影响玩家造成的伤害量。 (玩家类还需要知道这个变量是什么)。我已经被这个问题困扰了很长时间,非常感谢一些帮助!谢谢你!
Thank You for taking a look at my question!
In my game I have several scenes. I have an IntroScene, a HomeScene, and a PlayingScene. Now, if I need to create a variable int playerStrength and I want it to start of as being 0, but it needs to be displayed in my HomeScene and the PlayingScene needs to have the ability to access it as well, how can I do this? Because the HomeScene needs to display this integer, and the PlayingScene needs to know what it is so it can effect the amount of damage the player does. (And the players class would also need to know what this variable has to be). I have been suffering from this for a very long time and would REALLY appreciate some help! Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个封装此类信息的模型类,将其分配到您的应用程序委托中,并让所有其他人通过 [UIApplication sharedApplication].delegate 访问它。您可以对需要知道值何时更改的视图控制器进行键值观察 - 经典的 MVC 架构。
Create a model class encapsulating this kind of info, alloc it in your app delegate and have all others get to it via [UIApplication sharedApplication].delegate. You can do key value observing on it for view controllers that need to know when values change - classic MVC architecture.
我最近写了一篇关于这些类型的架构问题的教程:
http://www.hollance.com/2011/04/making-your-classes-talk-to-each-other-part-1/" rel="nofollow"> hollance.com/2011/04/making-your-classes-talk-to-each-other-part-1/
I recently wrote a tutorial on exactly these types of architectural issues:
http://www.hollance.com/2011/04/making-your-classes-talk-to-each-other-part-1/