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

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.

主站蜘蛛池模板: 漳浦县| 麦盖提县| 黑山县| 沙河市| 兴安盟| 永福县| 错那县| 张家港市| 阿荣旗| 宜兰市| 达孜县| 汤阴县| 怀仁县| 广灵县| 静乐县| 龙江县| 河源市| 南涧| 古丈县| 酉阳| 都兰县| 乌拉特后旗| 肇源县| 内丘县| 灌云县| 宁河县| 新密市| 乐都县| 林甸县| 瑞安市| 砚山县| 岗巴县| 寿宁县| 侯马市| 明溪县| 肥城市| 星子县| 乌苏市| 白水县| 嘉义市| 进贤县|