官术网_书友最值得收藏!

Transitioning between scenes

We will discuss how to transition between scenes in this section.

Getting ready

Most of the work is already done in the previous section, so let's dive right into creating the code. However, as we want to transition into GameplayScene, we have to import the class into the MainScene.m file, as follows:

#import "GameplayScene.h"

How to do it…

Next, add the following highlighted code to the playBtnPressed function in the MainScene.m file:

-(void)playBtnPressed:(id)sender{

  CCLOG(@"play button pressed");

 [[CCDirector sharedDirector] replaceScene:
 [[GameplayScene alloc] 
 initWithLevel:@"1"]];
}

How it works…

Now, when we press the play button, GameplayScene will load, display the GameplayScene text, and show that it has loaded level 1.

There's more…

We added a reset button and a function that will take us to MainMenu in GameplayScene. So, we will change the resetButtonPressed function in GameplayScene.m as follows so that when we press the reset button, the scene will transition to the MainMenu scene:

-(void)resetBtnPressed:(id)sender{

  CCLOG(@"reset button pressed");

  [[CCDirector sharedDirector] 
    replaceScene:[[MainScene alloc] init]];

}

Run and press the reset button to go back to the main menu now.

主站蜘蛛池模板: 樟树市| 临泉县| 闻喜县| 兴国县| 沈阳市| 苏尼特左旗| 宁明县| 刚察县| 聂拉木县| 南岸区| 利津县| 蛟河市| 西乌| 水城县| 大邑县| 易门县| 五常市| 鹿泉市| 辽宁省| 乌恰县| 商洛市| 冀州市| 法库县| 阿拉尔市| 沧州市| 博爱县| 马尔康县| 积石山| 泸西县| 崇州市| 甘德县| 苏州市| 延吉市| 通道| 巫溪县| 兰溪市| 仁化县| 泰兴市| 甘肃省| 公主岭市| 理塘县|