cocos2d-x 3.1.1 step by step 学习笔记7 场景与层

1:场景与层的关系:

一个场景里面可以有多个层,一个层只能对应一个场景

当清楚知道每个屏幕要显示的内容,并分别定义在不同的Node,然后统一添加在Sence中,执行 director->runWithScene(scene);就可以显示在屏幕了

 

2:场景与层初始化的顺序:

在AppDelegatge中  执行: auto scene = HelloWorld::createScene();

转到 HelloWorld.cpp

Scene* HelloWorld::createScene()
{
 // 'scene' is an autorelease object
 auto scene = Scene::create();

 // 'layer' is an autorelease object
 auto layer = HelloWorld::create();

 // add layer as a child to scene
 scene->addChild(layer);

 // return the scene
 return scene;
}

在HelloWorld中  创建Scene  创建Layer  然后把Layer加入到scene

 

3:场景切换相关函数

场景切换是通过导演类Director类来实现的,其中的相关函数如下:

 

void runWithScene(Scene * scene) .该函数可以运行场景,只能在启动第一个场景时运行该函数,如果已经有一个场景在运行就不能调用该函数

 

void replaceScene(Scenescene) 

Replaces the running scene with a new one.

The running scene is terminated. ONLY call it if there is a running scene.

切换到下一个场景,用一个新的场景替换当前的场景,当前场景被终端释放

 

void pushScene(Scenescene) 

Suspends the execution of the running scene, pushing it on the stack of suspended scenes.

The new scene will be executed. Try to avoid big stacks of pushed scenes to reduce memory allocation. ONLY call it if there is a running scene

. 将当前场景挂起放到场景堆栈当中,然后再切换到下一个场景当中

 

void popScene( ) 

Pops out a scene from the stack.

This scene will replace the running one. The running scene will be deleted. If there are no more scenes in the stack the execution is terminated. ONLY call it if there is a running scene.

与pushScene配合使用,回到上一个场景

 

void popToRootScene( ) 

Pops out all scenes from the stack until the root scene in the queue.

This scene will replace the running one. Internally it will call popToSceneStackLevel(1)

与pushScene配合使用,回到根场景

 

4:场景过渡动画

TransitionScene类

当场景在切换过程中可以通过带有动画功能的场景TransitionScene的子类来包装,要切换的场景就可以显示动画切换的效果。TransitionScene继承Scene,所有的

场景过渡效果的类都继承于TransitionScene

static TransitionScenecreate (float t, Scene *scene)
 creates a base transition with duration and incoming scene More...

实例代码:

从HelloWord场景转换到Setting场景:

auto sc = Setting::createScene();
 auto reScene = TransitionJumpZoom::create(1.0f, sc);
 Director::getInstance()->pushScene(reScene);

 

5:场景的生命周期

层(Layer)和Node生命周期函数如下:

bool Init()---初始化层调用

void onEnter()。 进入层的时候调用

void onEnterTransitionDidFinish().  进入层而且过渡动画结束的时候调用

void onExit()。退出层的时候调用

void onExitTransitionDidStart()。退出层而且开始过渡动画的时候开始调用

void cleanup()。 层对象被清楚的时候调用。

 

在多个场景切换时候,场景的生命周期会更加复杂。这一节我们介绍一下场景切换生命周期。

多个场景切换时候分为几种情况:

情况1,使用pushScene函数从实现HelloWorld场景进入Setting场景。

情况2,使用replaceScene函数实现从HelloWorld场景进入Setting场景。

情况3,使用popScene函数从实现Setting场景回到HelloWorld场景。

 

参考HelloWorld重写Setting层的中几个生命周期函数,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
bool Setting::init()  
{             
     if ( !Layer::init() )  
     {  
          returnfalse;  
     }  
     
     log ( "Settinginit" );  
     ......  
     returntrue;  
}  
     
void Setting::onEnter()  
{  
     Layer::onEnter();  
     log ( "SettingonEnter" );  
}  
     
void Setting::onEnterTransitionDidFinish()  
{  
     Layer::onEnterTransitionDidFinish();  
     log ( "SettingonEnterTransitionDidFinish" );  
}  
     
void Setting::onExit()  
{  
     Layer::onExit();  
     log ( "SettingonExit" );  
}  
     
void Setting::onExitTransitionDidStart()  
{  
     Layer::onExitTransitionDidStart();  
     log ( "SettingonExitTransitionDidStart" );  
}  
     
void Setting::cleanup()  
{  
     Layer::cleanup();  
     log ( "Settingcleanup" );  
}



情况1时候,它的调用顺序如下图所示。


情况2时候,它的调用顺序如下图所示,从图中可见与上面不同是多出HelloWorld中cleanup函数,这也说明replaceScene函数会释放场景对象。


情况3时候,它的调用顺序如下图所示,从图中可见popScene函数时候调用Setting中cleanup函数,这说明popScene函数会释放Setting场景对象,当回到HelloWorld场景时候并不会调用init()函数,而是调用onEnter函数。


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值