这是Cocos2dx最简单的部分。主要是体现对场景的操作,其实这东西就是Flash的舞台,安卓的Activity,WIN32窗体程序的Framework窗体,网页的body,反正就是对那个容纳各种东西的大容器进行操作,爱怎么叫就怎么叫。

百牛信息技术bainiu.ltd整理发布于博客园

用一个例子说明这个问题,将会做出如下的效果,在官方提供的Helloworld加一个场景Scene1,Scene1里面就摆一个可以切回Helloworld的按钮,同时设置这个Scene1为启动程序(游戏)的初始场景。同时对原本Helloworld场景的关闭按钮进行改造,原本关闭程序调整为切换到Scene1。两个场景切换有动画效果,当然这是Cocos2dx本来就自带的。

1、首先,与《【Cocos2dx】Windows平台下Cocos2dx 2.x的下载、安装、配置,打造自己的Helloworld》(点击打开链接)中一样,利用(cocos2d-x-2.2.6安装目录).\tools\project-creator下的create_project.py,输入:

  1. create_project.py -project Scene -package test.scene -language cpp

在(cocos2d-x-2.2.6安装目录).\project下得到一个Scene文件夹,打开其中的proj.win32中的HelloCpp.sln利用vs2010进行编辑。

2、上来直接新建场景Scene1,如下图,对HelloCpp下的Classes文件夹中,新建两个项一个Scene1.h,另一个为Scene1.cpp。

这里千万要注意的时,记得把这个文件创建在(工程目录).\Classes文件中,默认是坑爹的proj.win32,如果不创建在.\Classes文件夹中,你创建的文件夹,无法与原来就存在的文件使用include命令相互交互……

3、对Scene1.h编写如下的代码,技巧是模仿原来就存在的HelloWorldScene.h声明一个场景

  1. #ifndef __SCENE1_H__
  2. #define __SCENE1_H__
  3. #include "cocos2d.h"
  4. class Scene1:public cocos2d::CCLayer
  5. {
  6. public:
  7. // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
  8. virtual bool init();
  9. // there's no 'id' in cpp, so we recommend returning the class instance pointer
  10. static cocos2d::CCScene* scene();
  11. void menuGoToHelloworld(CCObject* pSender);//声明场景切换的按钮的回调(执行)函数
  12. //原本为HelloWorld这里改成Scene1
  13. CREATE_FUNC(Scene1);
  14. };
  15. #endif
  16. //这里仿造HelloWorldScene.h这个文件进行修改,把原本为HelloWorld都改成Scene1,此文件主要是场景的声明、按钮函数的声明

4、之后,对Scene1.cpp编写如下的代码,同样是模仿原来就存在的HelloWorldScene.cpp的关闭按钮,及其回调函数,也就是执行函数。

  1. #include "HelloWorldScene.h"//由于要切换回Helloworld这个场景,因此要声明这个函数
  2. #include "Scene1.h"
  3. USING_NS_CC;
  4. //声明部分,依旧仿造HelloWorldScene.h进行修改
  5. CCScene* Scene1::scene()
  6. {
  7. // 'scene' is an autorelease object
  8. CCScene *scene = CCScene::create();
  9. // 'layer' is an autorelease object
  10. Scene1 *layer = Scene1::create();
  11. // add layer as a child to scene
  12. scene->addChild(layer);
  13. // return the scene
  14. return scene;
  15. }
  16. //精华部分,场景组件的放置
  17. bool Scene1::init()
  18. {
  19. //声明位置组件,主要是为了下方确定位置的setPosition函数中ccp,origin等可以跨平台确定函数的组件可用
  20. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  21. CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
  22. //声明一个按钮
  23. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  24. "CloseNormal.png",//正常状态的图片,系统自带的
  25. "CloseSelected.png",//被点击的图片
  26. this,
  27. menu_selector(Scene1::menuGoToHelloworld));//声明按钮的回调(执行)函数,头文件已经声明过这个函数
  28. //按钮的位置
  29. pCloseItem->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height/2));
  30. //摆放按钮的固有实现部分,HelloWorldScene.cpp复制过来的,什么意思不用管
  31. // create menu, it's an autorelease object
  32. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  33. pMenu->setPosition(CCPointZero);
  34. this->addChild(pMenu);
  35. return true;
  36. }
  37. //按钮的回调(执行)函数的实现
  38. void Scene1::menuGoToHelloworld(CCObject* pSender)
  39. {
  40. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  41. CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  42. #else
  43. //核心在这句话,其余都是HelloWorldScene.cpp复制过来的,什么意思不用管,把原本的end()方法,改成切换场景replaceScene()方法。
  44. //CCTransitionMoveInL为左进入特效,0.4f为耗时,越少越快,可以为3.0f等,HelloWorld::scene()就是要切换到的场景
  45. CCDirector::sharedDirector()->replaceScene(CCTransitionMoveInL::create(0.4f,HelloWorld::scene()));
  46. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  47. exit(0);
  48. #endif
  49. #endif
  50. }

5、之后,同理,把在HelloworldScene.cpp中引入Scene1.h这个头文件。同时修改一个其关闭按钮的回调函数,在第86行,从原本的关闭,改为渐变切换特效。具体如下:

  1. #include "HelloWorldScene.h"
  2. #include "Scene1.h"//引入要切换的场景
  3. USING_NS_CC;
  4. CCScene* HelloWorld::scene()
  5. {
  6. // 'scene' is an autorelease object
  7. CCScene *scene = CCScene::create();
  8. // 'layer' is an autorelease object
  9. HelloWorld *layer = HelloWorld::create();
  10. // add layer as a child to scene
  11. scene->addChild(layer);
  12. // return the scene
  13. return scene;
  14. }
  15. // on "init" you need to initialize your instance
  16. bool HelloWorld::init()
  17. {
  18. //////////////////////////////
  19. // 1. super init first
  20. if ( !CCLayer::init() )
  21. {
  22. return false;
  23. }
  24. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  25. CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
  26. /////////////////////////////
  27. // 2. add a menu item with "X" image, which is clicked to quit the program
  28. //    you may modify it.
  29. // add a "close" icon to exit the progress. it's an autorelease object
  30. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  31. "CloseNormal.png",
  32. "CloseSelected.png",
  33. this,
  34. menu_selector(HelloWorld::menuCloseCallback));
  35. pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
  36. origin.y + pCloseItem->getContentSize().height/2));
  37. // create menu, it's an autorelease object
  38. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  39. pMenu->setPosition(CCPointZero);
  40. this->addChild(pMenu, 1);
  41. /////////////////////////////
  42. // 3. add your codes below...
  43. // add a label shows "Hello World"
  44. // create and initialize a label
  45. CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
  46. // position the label on the center of the screen
  47. pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
  48. origin.y + visibleSize.height - pLabel->getContentSize().height));
  49. // add the label as a child to this layer
  50. this->addChild(pLabel, 1);
  51. // add "HelloWorld" splash screen"
  52. CCSprite* pSprite = CCSprite::create("HelloWorld.png");
  53. // position the sprite on the center of the screen
  54. pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
  55. // add the sprite as a child to this layer
  56. this->addChild(pSprite, 0);
  57. return true;
  58. }
  59. void HelloWorld::menuCloseCallback(CCObject* pSender)
  60. {
  61. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  62. CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  63. #else
  64. CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.4f,Scene1::scene()));//核心的修改
  65. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  66. exit(0);
  67. #endif
  68. #endif
  69. }

这里,切换场景的特效,可以通过查询cocos2dx的API进行了解,常用的特效如下所示:

  1. //慢慢淡化到另一场景
  2. TransitionCrossFade::create(时间,目标场景);
  3. //本场景变暗消失后另一场景慢慢出现
  4. TransitionFade::create(时间,目标场景);
  5. //本场景右上角到左下角方块消失到另一场景
  6. TransitionFadeBL::create(时间,目标场景);
  7. //本场景从上到下横条消失到另一场景
  8. TransitionFadeDown::create(时间,目标场景);
  9. //本场景左下角到右上角方块消失到另一场景
  10. TransitionFadeTR::create(时间,目标场景);
  11. //本场景从下到上横条消失到另一场景
  12. TransitionFadeUp::create(时间,目标场景);
  13. //本场景翻转消失到另一场景(斜上方)
  14. TransitionFlipAngular::create(时间,目标场景,样式 );
  15. //本场景翻转消失到另一场景(X轴)
  16. TransitionFlipX::create(时间,目标场景,样式);
  17. //本场景翻转消失到另一场景(Y轴)
  18. TransitionFlipY::create(时间,目标场景);
  19. //本场景跳动消失后另一场景跳动出现
  20. TransitionJumpZoom::create(时间,目标场景);
  21. //另一场景由整体从下面出现
  22. TransitionMoveInB::create(时间,目标场景);
  23. //另一场景由整体从左面出现
  24. TransitionMoveInL::create(时间,目标场景);
  25. //另一场景由整体从上面出现
  26. TransitionMoveInT::create(时间,目标场景);
  27. //另一场景由整体从右面出现
  28. TransitionMoveInR::create(时间,目标场景);
  29. //翻页切换,bool为true是向前翻。
  30. TransitionPageTurn::create(时间,目标场景,bool);
  31. //本场景从左到右消失同时另一场景出现
  32. TransitionProgressHorizontal::create(时间,目标场景);
  33. //本场景从中间到四周消失同时另一场景出现
  34. TransitionProgressInOut::create(时间,目标场景);
  35. //本场景从四周到中间消失同时另一场景出现
  36. TransitionProgressOutIn::create(时间,目标场景);
  37. //本场景逆时针消失到另一场景
  38. TransitionProgressRadialCCW::create(时间,目标场景);
  39. //本场景顺时针消失到另一场景
  40. TransitionProgressRadialCW::create(时间,目标场景);
  41. //本场景从上到下消失同时另一场景出现
  42. TransitionProgressVertical::create(时间,目标场景);
  43. //本场景旋转消失后另一场景旋转出现
  44. TransitionRotoZoom::create(时间,目标场景);
  45. //本场景缩小切换到另一场景放大
  46. TransitionShrinkGrow::create(时间,目标场景);
  47. //本场景向上滑动到另一场景
  48. TransitionSlideInB::create(时间,目标场景);
  49. //本场景向右滑动到另一场景
  50. TransitionSlideInL::create(时间,目标场景);
  51. //本场景向左滑动到另一场景
  52. TransitionSlideInR::create(时间,目标场景);
  53. //本场景向下滑动到另一场景
  54. TransitionSlideInT::create(时间,目标场景);
  55. //本场景三矩形上下消失后另一场景三矩形上下出现
  56. TransitionSplitCols::create(时间,目标场景);
  57. //本场景三矩形左右消失后另一场景三矩形左右出现
  58. TransitionSplitRows::create(时间,目标场景);
  59. //本场景小方块消失到另一场景
  60. TransitionTurnOffTiles::create(时间,目标场景);
  61. //本场景翻转消失到另一场景(斜上方)
  62. TransitionZoomFlipAngular::create(时间,目标场景,样式);
  63. //本场景翻转消失到另一场景(X轴)
  64. TransitionZoomFlipX::create(时间,目标场景,样式);
  65. //本场景翻转消失到另一场景(Y轴)
  66. TransitionZoomFlipY::create(时间,目标场景,样式);

6、最后,修改程序开场所展示的场景就完事了,具体是在AppDelegate.cpp中,先引入我们创建的Scene1.h,同时将第29行的Helloworld改为Scene1,同时关闭第23行难看的提示信息。具体修改如下:

  1. #include "AppDelegate.h"
  2. #include "HelloWorldScene.h"
  3. #include "Scene1.h"
  4. USING_NS_CC;
  5. AppDelegate::AppDelegate() {
  6. }
  7. AppDelegate::~AppDelegate()
  8. {
  9. }
  10. bool AppDelegate::applicationDidFinishLaunching() {
  11. // initialize director
  12. CCDirector* pDirector = CCDirector::sharedDirector();
  13. CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
  14. pDirector->setOpenGLView(pEGLView);
  15. // turn on display FPS
  16. pDirector->setDisplayStats(false);
  17. // set FPS. the default value is 1.0/60 if you don't call this
  18. pDirector->setAnimationInterval(1.0 / 60);
  19. // create a scene. it's an autorelease object
  20. CCScene *pScene = Scene1::scene();//修改启动的场景为Scene1
  21. // run
  22. pDirector->runWithScene(pScene);
  23. return true;
  24. }
  25. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  26. void AppDelegate::applicationDidEnterBackground() {
  27. CCDirector::sharedDirector()->stopAnimation();
  28. // if you use SimpleAudioEngine, it must be pause
  29. // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  30. }
  31. // this function will be called when the app is active again
  32. void AppDelegate::applicationWillEnterForeground() {
  33. CCDirector::sharedDirector()->startAnimation();
  34. // if you use SimpleAudioEngine, it must resume here
  35. // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  36. }

【Cocos2dx】新建场景、场景的切换、设置启动场景与菜单的新建的更多相关文章

  1. CocosCreator设置启动场景

    刚开始接触CocosCreator,在调试时,如果有多个场景,不知道如何设置将某个指定的场景设置为启动场景,折腾了一圈,找到了设置的地方, 记录一下.   点击项目->项目设置     在预览运 ...

  2. cocos2d-x开发记录:二,基本概念(导演,场景,层和精灵,场景切换,效果)

    四,Director Scene Layer和Sprite(导演,场景,层和精灵) 1.Scenes(场景) 一个场景 (用CCScene对象实现)相当于APP工作流的独立部分.一些人也喜欢叫做“屏幕 ...

  3. Cocos2d-x 3.0final 终结者系列教程06-Director和场景跳转

    这些天互联网大事不少呀 1.逻辑思维分家(所谓合久必分,分久必合,实属正常.切行切珍惜吧) 2. 锤子手机开卖  (无论你买没买,反正我没买,作为多年Android开发的我深知说的亮点事实上在我看来都 ...

  4. Jmeter-常用线程组设置及场景运行时间计算

    Jmeter中通过线程组来模拟大用户并发场景,今天主要介绍三个常用的线程组,帮助我们设计更加完善的测试场景,另外介绍下场景执行时间如何计算. 一.Thread Group 取样器错误后要执行的动作   ...

  5. loadrunner场景之集合点设置技巧

    在loadrunner的虚拟用户中,术语concurrent(并发)和simultaneous(同时)存在一些区别,concurrent 是指虚拟场景中参于运行的虚拟用户. 而simultaneous ...

  6. Jmeter常用线程组设置及场景运行时间计算

    Jmeter中通过线程组来模拟大用户并发场景,今天主要介绍三个常用的线程组,帮助我们设计更加完善的测试场景,另外介绍下场景执行时间如何计算. 一.Thread Group 取样器错误后要执行的动作   ...

  7. Vuforia切换回识别场景后黑屏解决

    使用Vuforia SDK开发时,如果从其他非识别场景切换回识别场景,可能会出现黑屏问题. 解决方法是在切换到其他场景时,先将当前场景的Tracker信息全部Stop.代码如下: IEnumerato ...

  8. twitter storm源码走读之1 -- nimbus启动场景分析

    欢迎转载,转载时请注明作者徽沪一郎及出处,谢谢. 本文详细介绍了twitter storm中的nimbus节点的启动场景,分析nimbus是如何一步步实现定义于storm.thrift中的servic ...

  9. cocos2dx使用TiledMap创建斜45度地图场景

    做游戏,场景是一个很重要的部分,如果缺少这一步,很难做出好的游戏,对于cocos2dx来说,有很多2D的地图编辑器可以用,效果都还可以,其中Tiled是支持的比较好的,它支持Tiled编辑出来的几种模 ...

随机推荐

  1. [Bzoj3677][Apio2014]连珠线(树形dp)

    3677: [Apio2014]连珠线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 434  Solved: 270[Submit][Status] ...

  2. ArcCatalog中通过ArcSDE向Oracle数据库中导入数据

    将数据导入到Oracle指定的表空间的具体内容如下: 首先,在ArcCatalog中建立指定表空间的数据库连接(要以指定表空间的用户登录): 然后,在ArcCatlog中定位到数据源,选中并拷贝图层; ...

  3. android 长按弹出菜单,复制,粘贴,全选

    <!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...

  4. 时序数据库TSDB简单了解

    由于项目需要,简单看来下时序数据库: 时序数据库是针对大量数据写入.主要用于记录时序数据的,使用于监控记录的场景:写多读少场景: 什么是时序数据.时序数据是基于时间的一系列的数据.在有时间的坐标中将这 ...

  5. 什么场景应该用 MongoDB ?

    摘要: 月初在云栖社区上发起了一个 MongoDB 使用场景及运维管理问题交流探讨 的技术话题,有近5000人关注了该话题讨论,这里就 MongoDB 的使用场景做个简单的总结,谈谈什么场景该用 Mo ...

  6. ubuntu uninstall postgres

    Steps that worked for me on Ubuntu 8.04.2 to remove postgres 8.3 List All Postgres related packages ...

  7. Java多线程面试题归纳

    1.多线程有哪几种实现方法?举个样例说明下线程的同步. (1)Java多线程有两种实现方式:继承Thread类和实现Runnable接口,Thread就是实现了Runnable接口. 两个最简单的线程 ...

  8. DOM编程 --《高性能JavaScript》

    1.重绘和重排 浏览器下载完页面的所有组件 —— HTML标记,CSS,JavaScript,图片,会解析并生成两个内部数据结构. DOM树 表示页面结构 渲染树(CSS) 表示DOM节点如何显示 当 ...

  9. 【学生信息管理系统】EOF 和 BOF

    敲完学生信息管理系统时,在删除信息的时候,常常会出现下图这种错误,遇到问题就要解决这个问题.经过查阅理解了记录集Recordset的EOF和BOF属性,用这两个属性能够知道记录集中是否有信息存在. E ...

  10. c++学习笔记之基础---类内声明函数后在类外定义的一种方法

    在C++的“类”中经常遇到这样的函数, 返回值类型名 类名::函数成员名(参数表){ 函数体.} 双冒号的作用 ::域名解析符!返回值类型名 类名::函数成员名(参数表) { 函数体. } 这个是在类 ...