简单切换场景实例

来源:互联网 发布:vm虚拟机网络设置 编辑:程序博客网 时间:2024/05/16 11:29

PS:本人只是一名菜鸟,没有什么技巧可言,只是学习牛人们的经验一步一步的往前走.

1.首先申请一个新的场景类,和HelloWorld一样,先完成SecondScene.h头文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();  
    static cocos2d::CCScene* scene();
    void menuCloseCallback(CCObject* pSender);
    CREATE_FUNC(HelloWorld);
};
#endif 
注意:该头文件需要放在cocos2d-x-2.2.3\projects\HelloWorld\Classes目录下,要不后面会报错.


2.接着新建一个SecondScene.cpp文件实现头文件中所声明的函数
#include "SecondScene.h"
USING_NS_CC;
CCScene* SecondScene::scene()
{
    CCScene *scene = CCScene::create(); 
    SecondScene *layer = SecondScene::create(); 
    scene->addChild(layer); 
    return scene;
}
bool SecondScene::init()
{
    CCLabelTTF* pLabel = CCLabelTTF::create("Hi,Are you Kidd?", "Arial", 24);
pLabel->setPosition(ccp(200,200));
this->addChild(pLabel);
return true;
}


3.接着在HelloWorld场景中进行切换场景代码的增加
A.先增加SecondScene.h头文件的包含  
#include "SecondScene.h"
B.接着在退出按钮的地方做场景切换的操作
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()->end();
CCDirector::sharedDirector()->replaceScene(SecondScene::scene());  //[SecondScene.h头文件的位置要放对,要不这里报错]
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}


如果需要对第二场景的出现有动画效果的话,可以在menuCloseCallback修改成如下代码
CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInT::create(3.0f,SecondScene::scene())); //[从上面向下滑动,前面一个参数说明该动画所需要的时间
CCDirector::sharedDirector()->replaceScene(CCTransitionSlideInB::create(3.0f,SecondScene::scene())); //[从下面向上滑动]


本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/sunnykiddno1/article/details/41925189

0 0
原创粉丝点击