5

我已經在cocos2d 2.0中創建了一個項目,並且想要使用故事板來合併主菜單。如何將故事板合併到cocos2d 2.0項目中?

我已經試過傑羅德Putnam的教程在這裏tinytimgames.com(我不能提供的鏈接,因爲新用戶可在每個崗位只有2個鏈接,但如果谷歌「的cocos2d故事板」,它是第一個鏈接) 但它不適合我。我完全遵循(我認爲)。我打開了我的cocos2d項目,並從他的github,CCViewController.m和.h導入了這些文件,然後創建了一個新的故事板文件並遵循教程。然而,當我運行它時,它直接開始在我的cocos2d遊戲上,而不是剛剛創建的新菜單上。

我也試過這個教程: http://zackworkshopios.blogspot.com/2012/06/cocos2d-with-storyboard-example.html 但再次因爲我沒有它沒有爲我工作(或不知道在哪裏可以找到/領)libcocos2d.a和libCocosDenshion.a文件。

這是另一個教程中,我從fidgetware嘗試:http://fidgetware.com/Tutorials/page15/page15.html 我做了這個教程,但我的項目沒有一個叫RootViewController的文件(.M或.H),所以我不知道放在哪裏是應該的代碼進入這些文件。

也有Ray Wenderlich的教程,但他不使用故事板。

如果有人能給我一個解決方案,爲什麼這些都不是爲我工作,或者給我一步一步的詳細介紹如何將故事板併入我的cocos2d 2.0項目,我非常感謝。另外我的另一個問題是我應該從一個cocos2d 2.0項目開始,並且包含故事板,或者我應該從一個單視圖應用程序項目(或另一個項目)開始,併合並我的cocos2d 2.0部分。提前致謝!

回答

8

好吧,我設法最終弄清楚了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中。米複製並粘貼(從普特南的教程)

#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]]; 

確保用包含你的遊戲場景的名稱來代替「遊戲」。一旦你這樣做,去你AppDelegate.m以及與此更換你的

application didFinishLaunchingWithOptions 

功能:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    return YES; 
} 

你幾乎沒有!現在爲你的故事板文件在提供的鏈接中按照Putnam的教程。他在說「並將其類指派給我們剛剛創建的類」時,將其分配給cocos2dViewController。就是這樣!運行該項目,它應該工作,如果不是隨便問你有任何問題。

3

你會愛上我。

本教程http://www.tinytimgames.com/2012/02/07/cocos2d-and-storyboards/

那麼你得在此處設置

,關鍵是要瞄準你的主要情節串連圖板在你的iOS應用程序到一個你的主要故事板創建,在「項目名稱」 /夏日/ iPhone/iPod部署信息。選擇你的男人的故事板有

然後在AppDelegate.m刪除代碼,以便它看起來像這樣:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 


return YES; 
} 

,如果你不來的權利,我會給你我的工作的源代碼,只是讓我知道

+0

你可以發佈一個工作項目下載鏈接嗎? – 2013-08-08 12:48:43

+0

@OscarApeland我很抱歉,我從未回覆過你。不幸的是,我不再有這個項目,幾乎不能記得我當時所做的任何事情(現在團結起來)。請接受我的道歉。 – 2016-04-13 20:11:00

0

感謝bagelboy,我只有一個更新:

// Create the OpenGL view that Cocos2D will render to. 
    CCGLView *glView = [CCGLView viewWithFrame:self.view.frame 
            pixelFormat:kEAGLColorFormatRGB565 
            depthFormat:0 
          preserveBackbuffer:NO 
            sharegroup:nil 
           multiSampling:NO 
           numberOfSamples:0]; 

對於我的情況下,[[[UIApplication sharedApplication] keyWindow] bounds]沒有工作,我用self.view.frame

相關問題