- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- Siddharth Shekar
- 183字
- 2021-07-16 09:43:49
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.
- 編寫整潔的Python代碼(第2版)
- Animate CC二維動畫設計與制作(微課版)
- The React Workshop
- Mastering AndEngine Game Development
- Big Data Analytics
- QGIS:Becoming a GIS Power User
- Java Web程序設計任務教程
- JavaScript入門經典
- Xamarin Blueprints
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- ASP.NET開發寶典
- Mastering Object:Oriented Python(Second Edition)
- Learning TypeScript
- Java程序設計及應用開發
- 透視C#核心技術:系統架構及移動端開發