好吧,在 Jerrod Putnam 的帮助下我终于弄清楚了,所以谢谢 Jerrod!首先在这里查看他的教程:
http://www.tinytimgames.com/2012/02/07/cocos2d-and-storyboards/
并从 github 链接下载并导入文件。然后创建 CCViewController 的子类并将其命名为 cocos2dViewController。在 cocos2dViewController.h 中复制并粘贴以下内容:
#import "CCViewController.h"
@interface cocos2dViewController : CCViewController
@end
并在 cocos2dViewController.m 中复制并粘贴此内容(来自 Putnam 的教程)
#import "GamePlay.h"
#import "cocos2dViewController.h"
@interface cocos2dViewController ()
@end
@implementation cocos2dViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CCDirector *director = [CCDirector sharedDirector];
if([director isViewLoaded] == NO)
{
// Create the OpenGL view that Cocos2D will render to.
CCGLView *glView = [CCGLView viewWithFrame:[[[UIApplication sharedApplication] keyWindow] bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
// Assign the view to the director.
director.view = glView;
// Initialize other director settings.
[director setAnimationInterval:1.0f/60.0f];
[director enableRetinaDisplay:YES];
}
// Set the view controller as the director's delegate, so we can respond to certain events.
director.delegate = self;
// Add the director as a child view controller of this view controller.
[self addChildViewController:director];
// Add the director's OpenGL view as a subview so we can see it.
[self.view addSubview:director.view];
[self.view sendSubviewToBack:director.view];
// Finish up our view controller containment responsibilities.
[director didMoveToParentViewController:self];
// Run whatever scene we'd like to run here.
if(director.runningScene)
[director replaceScene:[GamePlay scene]];
else
[director pushScene:[GamePlay scene]];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
您会注意到我导入了 GamePlay.h,这是因为 GamePlay.m 是我的游戏的所有内容的地方。因此,请导入游戏的头文件。你也会看到我打电话
if(director.runningScene)
[director replaceScene:[GamePlay scene]];
else
[director pushScene:[GamePlay scene]];
确保将“GamePlay”替换为包含您的游戏的场景的名称。完成此操作后,转到您的 AppDelegate.m 并替换您的
application didFinishLaunchingWithOptions
与此功能:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
你快到了!现在,对于您的故事板文件,请按照提供的链接中的 Putnam 教程进行操作。他说“并将其类分配给我们刚刚创建的类”,将其分配给 cocos2dViewController。就是这样!运行该项目,它应该可以工作,如果没有,请随时提出您的任何问题。