背景

在学习马里奥时,我们学习到从菜单场景到游戏场景的切换,代码如下

  1. void CMMenuScene::OnStartCallBack( CCObject *pSender )
  2. {
  3. CCDirector* pDirector = CCDirector::sharedDirector();
  4. CCScene *pScene = CMGameScene::CreateGameScene();
  5. pDirector->replaceScene(pScene);
  6. }

上面的代码调用了CCDirector::replaceScene函数去进行场景切换。

CCDirector和CCScene

在cocos2dx里,CCDirector和CCScene有很密切的关系,CCDirector是导演,负责CCScene的展示,切换,结束等工作,具体体现在成员函数中:

  1. /** Enters the Director's main loop with the given Scene.
  2. * Call it to run only your FIRST scene.
  3. * Don't call it if there is already a running scene.
  4. *
  5. * It will call pushScene: and then it will call startAnimation
  6. */
  7. void runWithScene(CCScene *pScene);
  8.  
  9. /** Suspends the execution of the running scene, pushing it on the stack of suspended scenes.
  10. * The new scene will be executed.
  11. * Try to avoid big stacks of pushed scenes to reduce memory allocation.
  12. * ONLY call it if there is a running scene.
  13. */
  14. void pushScene(CCScene *pScene);
  15.  
  16. /** Pops out a scene from the queue.
  17. * This scene will replace the running one.
  18. * The running scene will be deleted. If there are no more scenes in the stack the execution is terminated.
  19. * ONLY call it if there is a running scene.
  20. */
  21. void popScene(void);
  22.  
  23. /** Pops out all scenes from the queue until the root scene in the queue.
  24. * This scene will replace the running one.
  25. * Internally it will call `popToSceneStackLevel(1)`
  26. */
  27. void popToRootScene(void);
  28.  
  29. /** Pops out all scenes from the queue until it reaches `level`.
  30. If level is 0, it will end the director.
  31. If level is 1, it will pop all scenes until it reaches to root scene.
  32. If level is <= than the current stack level, it won't do anything.
  33. */
  34. void popToSceneStackLevel(int level);
  35.  
  36. /** Replaces the running scene with a new one. The running scene is terminated.
  37. * ONLY call it if there is a running scene.
  38. */
  39. void replaceScene(CCScene *pScene);

以上是CCDirector的关于CCScene的成员函数

void runWithScene(CCScene *pScene):该函数是刚开始运行时,导演类展示场景

void pushScene(CCScene* scene):该函数将新的scene压入栈。在程序中有一个栈,可以保存很多场景,这样方便后退之类的操作。

void popScene(void):弹出,与pushScene对应

void popToRootScene(void):弹出栈中除了第一个场景外的其他场景

void popToSceneStackLevel(int level):弹出到制定位置的scene

void replaceScene(CCScene *pScene):让新的scene代替栈中当前的场景

动画切换

有的时候,简单的场景切换显得单调,如果需要动画切换,则需要以下类之一:

  1. HelloWorld2* scene = HelloWorld2::create();
  2. //CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, (CCScene*)scene));
  3. //CCDirector::sharedDirector()->replaceScene(CCTransitionFadeBL::create(1.2, (CCScene*)scene));
  4. //CCDirector::sharedDirector()->replaceScene(CCTransitionFadeTR::create(1.2, (CCScene*)scene));
  5. //CCDirector::sharedDirector()->replaceScene(CCTransitionTurnOffTiles::create(1.2, (CCScene*)scene));
  6. //CCDirector::sharedDirector()->replaceScene(CCTransitionTurnOffTiles::create(1.2, (CCScene*)scene));
  7. //CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.2, (CCScene*)scene));
  8. //CCDirector::sharedDirector()->replaceScene(CCTransitionMoveInL::create(1.2, (CCScene*)scene));
  9. //CCDirector::sharedDirector()->replaceScene(CCTransitionPageTurn::create(1.2, (CCScene*)scene, false));
  10. //CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::create(1.2, (CCScene*)scene));
  11. //CCDirector::sharedDirector()->replaceScene(CCTransitionShrinkGrow::create(1.2, (CCScene*)scene));
  12. //CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInL::create(1.2, (CCScene*)scene));
  13. CCDirector::sharedDirector()->replaceScene(CCTransitionCrossFade::create(1.2, (CCScene*)scene));

使用切换场景来进行场景切换,切换场景类如下所列:

CCTransitionRotoZoom//从大到小画面缩小并跳动进入
CCTransitionJumpZoom//立体从左边缩小跳动进入
CCTransitionMoveInL//从左边移动进入右边
CCTransitionMoveInR//从右边移动进入左边
CCTransitionMoveInT//从上边移动到入下边
CCTransitionMoveInB//从下边移动到入上边
CCTransitionSlideInL//从左边移动入右边
CCTransitionSlideInR//从右边移动入左边
CCTransitionSlideInT//从上边移动入下边
CCTransitionSlideInB//从下边移动入上边
CCTransitionShrinkGrow//从大到小在中间缩小进入
CCTransitionFlipX//从X轴方向立体翻转
CCTransitionFlipY//从Y轴方向立体翻转
CCTransitionFlipAngular//从右边头翻转进入
CCTransitionZoomFlipX//从X轴方向立体跳动翻转
CCTransitionZoomFlipY//从Y轴方向立体跳动翻转
CCTransitionZoomFlipAngular//从右边立体缩小翻转进入
CCTransitionFade//从中间渐变进入
CCTransitionCrossFade//从外围渐变进入
CCTransitionTurnOffTiles//从格子覆盖上层进入
CCTransitionSplitCols//竖直分三个方块切入
CCTransitionSplitRows//横向分三个方块切入
CCTransitionFadeTR//从左下角向右上角格子渐变进入
CCTransitionFadeBL//从右上角角向左下角格子渐变进入
CCTransitionFadeUp//从下向上渐变进入
CCTransitionFadeDown//从上向下渐变进入

cocos2dx进阶学习之场景切换的更多相关文章

  1. [Cocos2d-x For WP8]Transition 场景切换

    在游戏中通常会打完了一关之后就会从当前的场景转换到另外一关的场景了,在Cocos2d-x中是由CCScene类表示一个场景.那么场景(CCScene)是为游戏中的精灵(CCSprite)提供了舞台,场 ...

  2. cocos2dx进阶学习之CCNode

    继承关系 CCNode  -> CCObject CCNode在cocos2dx中抽象舞台对象,需要渲染的对象都是从CCNode派生,包括CCScene,CCLayer,CCSprite等等 C ...

  3. cocos2dx进阶学习之CCDirector

    继承关系 CCDirecotor -> CCObject, TypeInfo 处理主窗口消息,管理何时.何种方式执行场景. 经常被翻译成导演,负责管理整个游戏的进程推动和周边支持. 成员 inl ...

  4. Cocos2d-x Lua中多场景切换生命周期

    在多个场景切换时候,场景的生命周期会更加复杂.这一节我们介绍一下场景切换生命周期.多个场景切换时候分为几种情况:情况1,使用pushScene函数从实现GameScene场景进入SettingScen ...

  5. 【Cocos2d-x 3.x】 场景切换生命周期、背景音乐播放和场景切换原理与源码分析

    大部分游戏里有很多个场景,场景之间需要切换,有时候切换的时候会进行背景音乐的播放和停止,因此对这块内容进行了总结. 场景切换生命周期 场景切换用到的函数: bool Setting::init() { ...

  6. cocos2dx基础篇(24) 场景切换效果CCTransitionScene

    [3.x]     (1)去掉 "CC"     (2)卡牌翻转 TransitionFlip 中的样式 tOrientation // //1: kCCTransitionOri ...

  7. cocos2d-x入门学习篇;切换场景

    手机游戏开发最近很火爆,鉴于一直在学习c++,看起来上手就比较快了.这篇文章来自皂荚花 cocos2d-x技术,我把我的想法分享给大家. 首先来看一段代码: CCScene* HelloWorld:: ...

  8. cocos2dx进阶学习之CCScene

    继承关系 CCScene -> CCNode -> CCObject CCScene抽象了一个场景的概念,类似舞台的一幕 函数 static CCScene *create(void); ...

  9. cocos2d-x中常见的场景切换

    本文转载自:http://www.cnblogs.com/linux-ios/archive/2013/04/09/3010779.html bool HelloWorld::init() { /// ...

随机推荐

  1. C++类包含问题(重复包含和相互包含)

    一.重复包含头文件 头文件重复包含,可能会导致的错误包括:变量重定义,类型重定义及其他一些莫名其妙的错误.C++提供两种解决方案,分别是#ifndef和#pragma once #ifndef _SO ...

  2. LOJ 1370 Bi-shoe and Phi-shoe(欧拉函数的简单应用)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1370 题意:给你n个整数,第i个整数为Xi.定义phi(k)为k的欧拉函数值,设pi为 ...

  3. 简单的mvvm light 应用

      public  class MainStudentModel:ViewModelBase    { //实体        private StudentModel stu = new Stude ...

  4. [LeetCode]题解(python):003-Longest Substring Without Repeating Characters

    题目来源: https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题意分析: 题目是要求出最长的不 ...

  5. 前端笔试题 JS部分

    题目 http://www.itmian4.com/forum.php?mod=viewthread&tid=4540 http://www.itmian4.com/forum.php?mod ...

  6. QT不让windows休眠的方法

    对于一些Windows应用程序,必须要保证os不能休眠才能有效工作,如迅雷下载软件,如果os进入休眠,则会导致网络不正常,从而导致不能下载东西.那木有没有1种机制,当打开软件的时候,就自动将os设为不 ...

  7. 网页压缩gzip的问题及说明教程

    关于网页压缩gzip的问题及说明教程 最近比较多人反应gzip的问题 在wdcp的后台里已经有gzip功能的选项,也就是说,只要在这里开启了,就已支持 但从最近的问题中发现,基本上都是使用一些在线检测 ...

  8. 关于SpringMVC中找不到<mvc:resources/>标签的解决办法

    在springMVC中我们经常会用到<mvc:resources/>标签,但是有些编辑器中的schema过于陈旧.导致找不到<mvc:resources/>标签. 经过试验,有 ...

  9. 【科研论文】基于文件解析的飞行器模拟系统软件设计(应用W5300)

    摘要: 飞行器模拟系统是复杂飞行器研制和使用过程中的重要设备,它可以用来模拟真实飞行器的输入输出接口,产生与真实系统一致的模拟数据,从而有效避免因使用真实飞行器带来的高风险,极大提高地面测发控系统的研 ...

  10. 5种方法去掉HTML中Inline-Block元素之间的空白

    5种方法去掉HTML中Inline-Block元素之间的空白 记得年轻时我在IE6上开发,绝望的希望IE6能支持display: inline-block功能.当需要在”inline”元素上控制mar ...