【Cocos2dx】新建场景、场景的切换、设置启动场景与菜单的新建
这是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,输入:
- 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声明一个场景
- #ifndef __SCENE1_H__
- #define __SCENE1_H__
- #include "cocos2d.h"
- class Scene1:public cocos2d::CCLayer
- {
- public:
- // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
- virtual bool init();
- // there's no 'id' in cpp, so we recommend returning the class instance pointer
- static cocos2d::CCScene* scene();
- void menuGoToHelloworld(CCObject* pSender);//声明场景切换的按钮的回调(执行)函数
- //原本为HelloWorld这里改成Scene1
- CREATE_FUNC(Scene1);
- };
- #endif
- //这里仿造HelloWorldScene.h这个文件进行修改,把原本为HelloWorld都改成Scene1,此文件主要是场景的声明、按钮函数的声明
4、之后,对Scene1.cpp编写如下的代码,同样是模仿原来就存在的HelloWorldScene.cpp的关闭按钮,及其回调函数,也就是执行函数。
- #include "HelloWorldScene.h"//由于要切换回Helloworld这个场景,因此要声明这个函数
- #include "Scene1.h"
- USING_NS_CC;
- //声明部分,依旧仿造HelloWorldScene.h进行修改
- CCScene* Scene1::scene()
- {
- // 'scene' is an autorelease object
- CCScene *scene = CCScene::create();
- // 'layer' is an autorelease object
- Scene1 *layer = Scene1::create();
- // add layer as a child to scene
- scene->addChild(layer);
- // return the scene
- return scene;
- }
- //精华部分,场景组件的放置
- bool Scene1::init()
- {
- //声明位置组件,主要是为了下方确定位置的setPosition函数中ccp,origin等可以跨平台确定函数的组件可用
- CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
- //声明一个按钮
- CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
- "CloseNormal.png",//正常状态的图片,系统自带的
- "CloseSelected.png",//被点击的图片
- this,
- menu_selector(Scene1::menuGoToHelloworld));//声明按钮的回调(执行)函数,头文件已经声明过这个函数
- //按钮的位置
- pCloseItem->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height/2));
- //摆放按钮的固有实现部分,HelloWorldScene.cpp复制过来的,什么意思不用管
- // create menu, it's an autorelease object
- CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
- pMenu->setPosition(CCPointZero);
- this->addChild(pMenu);
- return true;
- }
- //按钮的回调(执行)函数的实现
- void Scene1::menuGoToHelloworld(CCObject* pSender)
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
- CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
- #else
- //核心在这句话,其余都是HelloWorldScene.cpp复制过来的,什么意思不用管,把原本的end()方法,改成切换场景replaceScene()方法。
- //CCTransitionMoveInL为左进入特效,0.4f为耗时,越少越快,可以为3.0f等,HelloWorld::scene()就是要切换到的场景
- CCDirector::sharedDirector()->replaceScene(CCTransitionMoveInL::create(0.4f,HelloWorld::scene()));
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- exit(0);
- #endif
- #endif
- }
5、之后,同理,把在HelloworldScene.cpp中引入Scene1.h这个头文件。同时修改一个其关闭按钮的回调函数,在第86行,从原本的关闭,改为渐变切换特效。具体如下:
- #include "HelloWorldScene.h"
- #include "Scene1.h"//引入要切换的场景
- USING_NS_CC;
- CCScene* HelloWorld::scene()
- {
- // 'scene' is an autorelease object
- CCScene *scene = CCScene::create();
- // 'layer' is an autorelease object
- HelloWorld *layer = HelloWorld::create();
- // add layer as a child to scene
- scene->addChild(layer);
- // return the scene
- return scene;
- }
- // on "init" you need to initialize your instance
- bool HelloWorld::init()
- {
- //////////////////////////////
- // 1. super init first
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
- /////////////////////////////
- // 2. add a menu item with "X" image, which is clicked to quit the program
- // you may modify it.
- // add a "close" icon to exit the progress. it's an autorelease object
- CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
- "CloseNormal.png",
- "CloseSelected.png",
- this,
- menu_selector(HelloWorld::menuCloseCallback));
- pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
- origin.y + pCloseItem->getContentSize().height/2));
- // create menu, it's an autorelease object
- CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
- pMenu->setPosition(CCPointZero);
- this->addChild(pMenu, 1);
- /////////////////////////////
- // 3. add your codes below...
- // add a label shows "Hello World"
- // create and initialize a label
- CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
- // position the label on the center of the screen
- pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
- origin.y + visibleSize.height - pLabel->getContentSize().height));
- // add the label as a child to this layer
- this->addChild(pLabel, 1);
- // add "HelloWorld" splash screen"
- CCSprite* pSprite = CCSprite::create("HelloWorld.png");
- // position the sprite on the center of the screen
- pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
- // add the sprite as a child to this layer
- this->addChild(pSprite, 0);
- return true;
- }
- void HelloWorld::menuCloseCallback(CCObject* pSender)
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
- CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
- #else
- CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.4f,Scene1::scene()));//核心的修改
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- exit(0);
- #endif
- #endif
- }
这里,切换场景的特效,可以通过查询cocos2dx的API进行了解,常用的特效如下所示:
- //慢慢淡化到另一场景
- TransitionCrossFade::create(时间,目标场景);
- //本场景变暗消失后另一场景慢慢出现
- TransitionFade::create(时间,目标场景);
- //本场景右上角到左下角方块消失到另一场景
- TransitionFadeBL::create(时间,目标场景);
- //本场景从上到下横条消失到另一场景
- TransitionFadeDown::create(时间,目标场景);
- //本场景左下角到右上角方块消失到另一场景
- TransitionFadeTR::create(时间,目标场景);
- //本场景从下到上横条消失到另一场景
- TransitionFadeUp::create(时间,目标场景);
- //本场景翻转消失到另一场景(斜上方)
- TransitionFlipAngular::create(时间,目标场景,样式 );
- //本场景翻转消失到另一场景(X轴)
- TransitionFlipX::create(时间,目标场景,样式);
- //本场景翻转消失到另一场景(Y轴)
- TransitionFlipY::create(时间,目标场景);
- //本场景跳动消失后另一场景跳动出现
- TransitionJumpZoom::create(时间,目标场景);
- //另一场景由整体从下面出现
- TransitionMoveInB::create(时间,目标场景);
- //另一场景由整体从左面出现
- TransitionMoveInL::create(时间,目标场景);
- //另一场景由整体从上面出现
- TransitionMoveInT::create(时间,目标场景);
- //另一场景由整体从右面出现
- TransitionMoveInR::create(时间,目标场景);
- //翻页切换,bool为true是向前翻。
- TransitionPageTurn::create(时间,目标场景,bool);
- //本场景从左到右消失同时另一场景出现
- TransitionProgressHorizontal::create(时间,目标场景);
- //本场景从中间到四周消失同时另一场景出现
- TransitionProgressInOut::create(时间,目标场景);
- //本场景从四周到中间消失同时另一场景出现
- TransitionProgressOutIn::create(时间,目标场景);
- //本场景逆时针消失到另一场景
- TransitionProgressRadialCCW::create(时间,目标场景);
- //本场景顺时针消失到另一场景
- TransitionProgressRadialCW::create(时间,目标场景);
- //本场景从上到下消失同时另一场景出现
- TransitionProgressVertical::create(时间,目标场景);
- //本场景旋转消失后另一场景旋转出现
- TransitionRotoZoom::create(时间,目标场景);
- //本场景缩小切换到另一场景放大
- TransitionShrinkGrow::create(时间,目标场景);
- //本场景向上滑动到另一场景
- TransitionSlideInB::create(时间,目标场景);
- //本场景向右滑动到另一场景
- TransitionSlideInL::create(时间,目标场景);
- //本场景向左滑动到另一场景
- TransitionSlideInR::create(时间,目标场景);
- //本场景向下滑动到另一场景
- TransitionSlideInT::create(时间,目标场景);
- //本场景三矩形上下消失后另一场景三矩形上下出现
- TransitionSplitCols::create(时间,目标场景);
- //本场景三矩形左右消失后另一场景三矩形左右出现
- TransitionSplitRows::create(时间,目标场景);
- //本场景小方块消失到另一场景
- TransitionTurnOffTiles::create(时间,目标场景);
- //本场景翻转消失到另一场景(斜上方)
- TransitionZoomFlipAngular::create(时间,目标场景,样式);
- //本场景翻转消失到另一场景(X轴)
- TransitionZoomFlipX::create(时间,目标场景,样式);
- //本场景翻转消失到另一场景(Y轴)
- TransitionZoomFlipY::create(时间,目标场景,样式);
6、最后,修改程序开场所展示的场景就完事了,具体是在AppDelegate.cpp中,先引入我们创建的Scene1.h,同时将第29行的Helloworld改为Scene1,同时关闭第23行难看的提示信息。具体修改如下:
- #include "AppDelegate.h"
- #include "HelloWorldScene.h"
- #include "Scene1.h"
- USING_NS_CC;
- AppDelegate::AppDelegate() {
- }
- AppDelegate::~AppDelegate()
- {
- }
- bool AppDelegate::applicationDidFinishLaunching() {
- // initialize director
- CCDirector* pDirector = CCDirector::sharedDirector();
- CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
- pDirector->setOpenGLView(pEGLView);
- // turn on display FPS
- pDirector->setDisplayStats(false);
- // set FPS. the default value is 1.0/60 if you don't call this
- pDirector->setAnimationInterval(1.0 / 60);
- // create a scene. it's an autorelease object
- CCScene *pScene = Scene1::scene();//修改启动的场景为Scene1
- // run
- pDirector->runWithScene(pScene);
- return true;
- }
- // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
- void AppDelegate::applicationDidEnterBackground() {
- CCDirector::sharedDirector()->stopAnimation();
- // if you use SimpleAudioEngine, it must be pause
- // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- }
- // this function will be called when the app is active again
- void AppDelegate::applicationWillEnterForeground() {
- CCDirector::sharedDirector()->startAnimation();
- // if you use SimpleAudioEngine, it must resume here
- // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- }

【Cocos2dx】新建场景、场景的切换、设置启动场景与菜单的新建的更多相关文章
- CocosCreator设置启动场景
刚开始接触CocosCreator,在调试时,如果有多个场景,不知道如何设置将某个指定的场景设置为启动场景,折腾了一圈,找到了设置的地方, 记录一下. 点击项目->项目设置 在预览运 ...
- cocos2d-x开发记录:二,基本概念(导演,场景,层和精灵,场景切换,效果)
四,Director Scene Layer和Sprite(导演,场景,层和精灵) 1.Scenes(场景) 一个场景 (用CCScene对象实现)相当于APP工作流的独立部分.一些人也喜欢叫做“屏幕 ...
- Cocos2d-x 3.0final 终结者系列教程06-Director和场景跳转
这些天互联网大事不少呀 1.逻辑思维分家(所谓合久必分,分久必合,实属正常.切行切珍惜吧) 2. 锤子手机开卖 (无论你买没买,反正我没买,作为多年Android开发的我深知说的亮点事实上在我看来都 ...
- Jmeter-常用线程组设置及场景运行时间计算
Jmeter中通过线程组来模拟大用户并发场景,今天主要介绍三个常用的线程组,帮助我们设计更加完善的测试场景,另外介绍下场景执行时间如何计算. 一.Thread Group 取样器错误后要执行的动作 ...
- loadrunner场景之集合点设置技巧
在loadrunner的虚拟用户中,术语concurrent(并发)和simultaneous(同时)存在一些区别,concurrent 是指虚拟场景中参于运行的虚拟用户. 而simultaneous ...
- Jmeter常用线程组设置及场景运行时间计算
Jmeter中通过线程组来模拟大用户并发场景,今天主要介绍三个常用的线程组,帮助我们设计更加完善的测试场景,另外介绍下场景执行时间如何计算. 一.Thread Group 取样器错误后要执行的动作 ...
- Vuforia切换回识别场景后黑屏解决
使用Vuforia SDK开发时,如果从其他非识别场景切换回识别场景,可能会出现黑屏问题. 解决方法是在切换到其他场景时,先将当前场景的Tracker信息全部Stop.代码如下: IEnumerato ...
- twitter storm源码走读之1 -- nimbus启动场景分析
欢迎转载,转载时请注明作者徽沪一郎及出处,谢谢. 本文详细介绍了twitter storm中的nimbus节点的启动场景,分析nimbus是如何一步步实现定义于storm.thrift中的servic ...
- cocos2dx使用TiledMap创建斜45度地图场景
做游戏,场景是一个很重要的部分,如果缺少这一步,很难做出好的游戏,对于cocos2dx来说,有很多2D的地图编辑器可以用,效果都还可以,其中Tiled是支持的比较好的,它支持Tiled编辑出来的几种模 ...
随机推荐
- OpenCV、PCL;Xtion、kinect;OpenNI、kinect for windows SDK比较
一.对比介绍: 1. OpenCV:开源跨平台,OpenCV于1999年由Intel建立,如今由Willow Garage提供支持. 2. OpenNI:OpenNI组织创建于2010年11月.主要成 ...
- css可见性
overflow:hidden: 溢出隐藏 visibility:hidden: 隐藏元素,隐藏之后还占据原来的位置 display:none: 隐藏元 ...
- Camtasia Studio如何添加画中画
将录像文件和其他视频文件拖放到剪辑箱,右击录像文件(camrec文件)添加到时间轴,一般这个就是主要的视频文件,我们会在这个基础上添加字幕,配音,画中画等,拖进去之后可以发现多出来了一个视频1和音频1 ...
- 小老虎CSDN博客流量分析
小老虎CSDN博客流量分析 一.分析的博客对象 http://blog.csdn.net/littletigerat 二.分析的时间节点 2014年7月10日星期四 三.PV.UV以及IP值 wa ...
- 6.非关系型数据库(Nosql)之mongodb:集群(主从复制)
1.主从复制是MongoDB最经常使用的复制方式.这样的方式很灵活,可用于备份.故障恢复.读扩展等 2最主要的设置方式就是建立一个主节点和一个或多个从节点,每一个从节点要知道主节点的地址. 执行 ...
- 1.shell编程之变量的高级用法
1.1.变量替换 变量替换的六种形式 实例:非贪婪和贪婪的区别 从头部删除 [root@VM_0_9_centos shell_learn]# var_1="i love you,do yo ...
- FSB总线、HT总线、QPI总线、DMI总线
http://wenku.baidu.com/link?url=d0NQFyvVpU--FpQUIjkPcdE036riitKgKib3385qr7VdB1oNUcIDpRndY-HH4ITjzJln ...
- 记录:50多行程序中找出多写的一个字母e
小霍同学调程序,做的是第11周的项目1 - 存储班长信息的学生类,可是她写的程序(就在以下),呃,请读者自己执行一下吧.(下午在机房调试时用的是Code::Blocks10.05.输出的是非常长的莫名 ...
- hive:Access denied for user 'root'@'%'
配置hive全分布模式时候,在mysql里面创建用户:create user 'hive' identified by 'hive'; 然后给hive帐号分配全部权限: grant all privi ...
- 创建一个zookeeper的会话(实现watcher)
在先前的章节中,我们利用zkCli去了解了一下主要的zookeeper的操作.在接下来的章节中,我们将会学习一下在应用中是怎样利用zookeeper的api的.接下来我们将利用一个程序展示一下,怎样来 ...