Iphone 全球游戏引擎?这是正确的方法吗
我正在试验一个简单的游戏。只显示几个按钮和文本。 我想将游戏引擎与视图、视图控制器等完全分离。因此,我在名为GameEngine的单独文件和类中定义了它 我在AppDelegate中声明并初始化GameEngine实例,如下所示:Iphone 全球游戏引擎?这是正确的方法吗,iphone,objective-c,Iphone,Objective C,我正在试验一个简单的游戏。只显示几个按钮和文本。 我想将游戏引擎与视图、视图控制器等完全分离。因此,我在名为GameEngine的单独文件和类中定义了它 我在AppDelegate中声明并初始化GameEngine实例,如下所示: #import "GameEngine.h" GameEngine *this_game; //It's global! @interface ...{ ... // this_game is not declared here at all ... } //
#import "GameEngine.h"
GameEngine *this_game; //It's global!
@interface ...{
...
// this_game is not declared here at all
...
}
//No getter/setter defined for this_game
#import "MyGameAppDelegate.h"
@implementation ...
// this_game not synthesized
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
this_game = [[GameEngine alloc] init];
this_game.managedObjectContext = self.managedObjectContext;
[this_game initialize_data];
[window addSubview:game_vc.view]; // Display the primary application view
[window makeKeyAndVisible];
}
...
-(void)dealloc{
...
[this_game release];
...
}
AppDelegate实现如下所示:
#import "GameEngine.h"
GameEngine *this_game; //It's global!
@interface ...{
...
// this_game is not declared here at all
...
}
//No getter/setter defined for this_game
#import "MyGameAppDelegate.h"
@implementation ...
// this_game not synthesized
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
this_game = [[GameEngine alloc] init];
this_game.managedObjectContext = self.managedObjectContext;
[this_game initialize_data];
[window addSubview:game_vc.view]; // Display the primary application view
[window makeKeyAndVisible];
}
...
-(void)dealloc{
...
[this_game release];
...
}
好的,这样一来,主视图控制器的头文件中会发生以下情况:
#import "GameEngine.h"
extern GameEngine *this_game;
// No other reference to this_game in the .h file
现在是.m中的代码:
-(void)viewDidLoad{
...
NSLog([this_game test], nil); //Just a quick "I am here" test
...
}
这一切都很好,我可以从程序中的各个点整天调用全局This_游戏对象的方法。它起作用了
问题实际上是关于在Objective-C的公认实践中如何“恰当”或恰当,如果您愿意,这种方法可能是什么
这是个坏主意吗?为什么?
有更好的办法吗?为什么更好
顺便说一句,我的目的是分离游戏引擎代码,以使其更易于在该平台和其他移动平台上重用。所有本机窗口和UI的东西都需要远离游戏逻辑
谢谢
-Martin我完全同意你的观点,将游戏逻辑代码与所有GUI相关内容分离始终是一个好主意:-)我认为你的方法乍一看似乎不错,但我想到了两个问题: 1)为什么不使用单亲家庭?因此,您不必在Obj-C项目中使用C代码,并且如果您需要在模拟器上运行时关注特殊情况,或者希望使用实体模型进行测试,或者诸如此类的话,您可以随时扩展您的应用程序。看一看。有一个很好的小头文件synthesingleton.h,我经常在我的项目中使用
< P > 2)如果你对1的回答是“提到的,因为我想在不同的系统上运行它……”,那么我建议把你的GAMEngEngress类实现为C或C++。因此,迁移到其他平台将更容易。Objective-C可能会成为跨平台的,如果苹果想这样做的话。但是他们(还没有?)。。。我建议对Kay提到的游戏逻辑代码使用C/C++来重用它们,这也是我的想法。现在我正在试验结构的想法,所以我把它放在Objective-C中。我想删除GameEngine中的CoreData持久性代码,并将其拉到其他特定于平台的模块中。GameEngine访问这些服务时应该完全不知道它在哪里运行。