zoukankan      html  css  js  c++  java
  • cocos2d-x入门学习篇;切换场景

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

    首先来看一段代码:

    CCScene* HelloWorld::scene()
    {
        CCScene * scene = NULL;
        do 
        {
            // 'scene' is an autorelease object
            scene = CCScene::create();
            CC_BREAK_IF(! scene);
    
            // 'layer' is an autorelease object
            HelloWorld *layer = HelloWorld::create();
            CC_BREAK_IF(! layer);
    
            // add layer as a child to scene
            scene->addChild(layer);
        } while (0);
    
        // return the scene
       

    这段代码是建立主场景核心代码,工程在新建后已经帮我们建好了。

    bool HelloWorld::init()
    {
    bool  bRet=false;
    do 
    {
        CC_BREAK_IF(!CCLayer::init());
        CCLabelTTF *ttf=CCLabelTTF::create("next scene","Arial",32);     //创建文字菜单
        CCMenuItemLabel *lableMenu=CCMenuItemLabel::create(ttf,this,menu_selector(HelloWorld::menuCloseCallback));
        CCMenu *menu=CCMenu::create(lableMenu,NULL);
        this->addChild(menu);      //加到主场景中
        bRet=true;
    } while (0);

    这段代码主要是在HelloWorld场景中做些事情,包括文字的创建,按键触发menuCloseCallback。

    再看按键触发的代码,其基本思想就是马上转换到第二个场景,里面可以实现一些特效。

    void HelloWorld::menuCloseCallback(CCObject* pSender)
    {
        // "close" menu item clicked
       // CCDirector::sharedDirector()->end();
        //调用下一个场景
        CCTransitionFade  *fade=CCTransitionFade::create(2.0,SecondScene::scene(),ccc3(255,100,100));
        CCDirector::sharedDirector()->replaceScene(fade);
    }

    在上面的 CCTransitionFade的静态函数create中创建SecondScene场景,cc3表示rgb颜色,表示淡入淡出的效果,这个你可以随意改变。

    接下来我们来看看第二个场景的代码,首先新建一个头文件和cpp文件,名字命名为SecondScene,其中头文件代码如下:

    #ifndef __SECOND_SCENE_H__
    #define __SECOND_SCENE_H__
    
    #include "cocos2d.h"
    
    #include "SimpleAudioEngine.h"
    
    class SecondScene : 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 recommand to return the exactly class pointer
        static cocos2d::CCScene* scene();
    
        // a selector callback
        void changeScene(CCObject* pSender);    //转换到第一个场景
    
        // implement the "static node()" method manually
        CREATE_FUNC(SecondScene);
    };

    其代码和第一个场景的代码差不多一致,只改变了场景变换的函数,把它命名为changeScene。最要的看它的场景变换是怎么实现的,在看它的cpp代码:

    bool  SecondScene::init()
    {
       bool    bRet=false;
    
        do 
        {
            CC_BREAK_IF(!CCLayer::init());
            CCLabelTTF *ttf=CCLabelTTF::create("first scene","Arial",34);
            CCMenuItemLabel *lableMenu=CCMenuItemLabel::create(ttf,this,menu_selector(SecondScene::changeScene));     //绑定到changeScene,按键触发
            CCMenu *menu=CCMenu::create(lableMenu,NULL);
            this->addChild(menu);
            bRet=true;
        } while (0);
        return   bRet;
    }

    void SecondScene::changeScene(CCObject* pSender)
    {

    CCDirector::sharedDirector()->setDepthTest(true);    //先设置摄像头
    CCTransitionPageTurn *pageTurn=CCTransitionPageTurn::create(2.0,HelloWorld::scene(),false);   
    CCDirector::sharedDirector()->replaceScene(pageTurn);   //进行翻页,翻到上一页
    //CCDirector::sharedDirector()->popScene();    //直接上这句代码的话特效消失,就直接从第二个场景变到第一个场景。

    }

     

    总结:从代码来看,我们要把握在场景初始化的时候对菜单项的基本加载,进入到下一个场景,其实就是新建一个类来实现了,充分利用c++面向对象的特性,掌握好cocos2dx的一些常用API函数,开发起来就比较简单了。总之,多思考,多写代码。

    作者:曹麒文

    出处:http://www.cnblogs.com/master-image/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面

  • 相关阅读:
    set desktop for aliyun ubuntu
    【深度学习】paddlepaddle——基于卷积神经网络的手写字识别案例
    【深度学习】paddlepaddle基础语法
    【深度学习】TensorFlow——理解张量
    【深度学习】TensorFlow——图详解
    【深度学习】TensorFlow——变量op
    【深度学习】TensorFlow——实现线性回归案例
    【深度学习】TensorFlow——理解会话
    【深度学习】TensorFlow——初识tensorflow
    【机器学习】量化策略
  • 原文地址:https://www.cnblogs.com/master-image/p/3831645.html
Copyright © 2011-2022 走看看