2015-01-04 151 views
0

所以我真的很想搞清楚如何在按下按鈕時切換場景。基本上,我想製作一個「開始遊戲」按鈕。有很多谷歌的答案,他們似乎都沒有幫助。無論是語法錯誤還是應用程序崩潰。無論如何,這是我的目標。我有一個gameviewcontroller,正確地讓我的按鈕,併成功地與按鈕按下(用於打印一個字符到控制檯)的方法進行通信。現在,在我的Title.m(我在開始現場),我有像這樣的方法:點擊切換場景的按鈕

-(void) gScene { 
    GameScene* gameScene = [[GameScene alloc] initWithSize:self.size]; 
    [self.view presentScene:gameScene]; 
} 

我也有方法,我Title.h,這是導入GameViewController。 在我的GameViewController中,與我按鈕按下鏈接的方法稱爲startGame。這就是startGame的樣子:

-(void)startGame { 
    Title* title = [[Title alloc] init]; 
    [title gScene]; 
} 

我希望在按鈕按下時,場景從標題切換到我的GameScene。我究竟做錯了什麼?

+0

你必須用接口初始化你的視圖控制器。你在使用筆尖還是故事板? –

回答

0

如果使用筆尖:

GameScene *vc = [[GameScene alloc] init]; 
[self presentViewController:vc animated:YES completion:nil]; 

如果使用故事板:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YOUR_STORYBOARD_NAME" bundle:nil]; 
GameScene *vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_IB_VC"]; 
[self presentViewController:vc animated:YES completion:nil]; 

如果以編程方式創建的情景:(Apple Documentation)

UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    levelViewController = [[LevelViewController alloc] init]; 
    window.rootViewController = levelViewController; 
    [window makeKeyAndVisible]; 

你的問題出在「self.size 「代碼用於設置框架的邊界,第三個例子可以幫助你rs

+0

我編程做了一切。我沒有使用筆尖或故事板。我很欣賞這種迴應,但仍然有點失落。 –

+0

感謝您的回覆,但我仍不確定如何應用您發佈的以編程方式創建的場景代碼。我是否會在gScene方法中放置該代碼(或者更確切地說,它應用於我的項目中的代碼),還是應該將它放在startGame方法中並完全放棄gScene方法?這是我想要的: UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; levelViewController = [[Title alloc] init]; window.rootViewController = levelViewController; [window makeKeyAndVisible]; –

+0

對不起,上面的格式,ctrl k(或mac等效,或命令k)由於某種原因不工作。無論如何,我嘗試並遇到問題:該levelViewController是一個未聲明的標識符,並且標題與UIViewController不兼容的類型。出於好奇心和同樣的問題,我用GameViewController替換Title。我不確定如何應用您的代碼,並且我已閱讀文檔。 –