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

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.

主站蜘蛛池模板: 安吉县| 敦煌市| 鄯善县| 东乌珠穆沁旗| 乃东县| 佛山市| 东辽县| 巩留县| 庆城县| 青阳县| 嘉义县| 桃源县| 格尔木市| 兰考县| 长武县| 吉首市| 黎城县| 天等县| 五家渠市| 定陶县| 城固县| 西城区| 长兴县| 颍上县| 乐陵市| 仲巴县| 喀喇沁旗| 巴楚县| 出国| 石门县| 广东省| 孟州市| 山东省| 绥德县| 平昌县| 荥经县| 恩平市| 衡东县| 浙江省| 富阳市| 科技|