• cocos2d-x——在一个cpp中展示多个场景


    //20秒后自动运行下一个场景
    runAction( CCSequence::create(CCDelayTime::create(20.0f),
    			CCCallFunc::create(this, callfunc_selector(GameOver::removeThis)),NULL));
    void GameOver::removeThis()
    {
    	nextCallback(this);
    }


    .h文件

    #ifndef __HELLOWORLD_SCENE_H__
    #define __HELLOWORLD_SCENE_H__
    
    #include "cocos2d.h"
    
    #include "Box2D/Box2D.h"
    
    #include "SimpleAudioEngine.h"
    
    class HelloWorld : 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();
    
        void menuCloseCallback(CCObject* pSender);
    	void restartCallback(CCObject* pSender);
    	void nextCallback(CCObject* pSender);
    	void backCallback(CCObject* pSender);
    	void Test();
        CREATE_FUNC(HelloWorld);
    };
    class Test1 : public HelloWorld
    {
    public:
    	Test1();
    };
    class Test2 : public HelloWorld
    {
    public:
    	Test2();
    };
    class Test3 : public HelloWorld
    {
    public:
    	Test3();
    };
    #endif  // __HELLOWORLD_SCENE_H__
    .cpp文件
    #include "HelloWorldScene.h"
    
    using namespace cocos2d;
    
    CCLayer* nextSpriteTestAction();
    CCLayer* backSpriteTestAction();
    CCLayer* restartSpriteTestAction();
    static int sceneIdx = -1; 
    typedef CCLayer* (*NEWDRAWPRIMITIVESFUNC)();
    #define DRAWPRIMITIVES_CREATE_FUNC(className) 
    	static CCLayer* create##className() 
    { return new className(); }
    
    DRAWPRIMITIVES_CREATE_FUNC(Test1);
    DRAWPRIMITIVES_CREATE_FUNC(Test2);
    DRAWPRIMITIVES_CREATE_FUNC(Test3);
    static NEWDRAWPRIMITIVESFUNC createFunctions[] =
    {
    	createTest1,
    	createTest2,
    	createTest3,
    };
    #define MAX_LAYER  (sizeof(createFunctions) / sizeof(createFunctions[0]))
    static CCLayer* nextAction()
    {
    	sceneIdx++;
    	sceneIdx = sceneIdx % MAX_LAYER;
    	CCLayer* pLayer = (createFunctions[sceneIdx])();
    	pLayer->autorelease();
    	return pLayer;
    }
    static CCLayer* backAction()
    {
    	sceneIdx--;
    	int total = MAX_LAYER;
    	if( sceneIdx < 0 )
    		sceneIdx += total;
    	CCLayer* pLayer = (createFunctions[sceneIdx])();
    	pLayer->autorelease();
    	return pLayer;
    }
    static CCLayer* restartAction()
    {
    		CCLayer* pLayer = (createFunctions[sceneIdx])();
    		pLayer->autorelease();
    		return pLayer;
    }
    void HelloWorld::restartCallback(cocos2d::CCObject *pSender)
    {
    	if (sceneIdx != -1)
    	{
    		CCScene *s = new CCScene;
    		s->addChild(restartAction());
    		CCDirector::sharedDirector()->replaceScene(s);
    		s->release();
    	}
    }
    void HelloWorld::nextCallback(cocos2d::CCObject *pSender)
    {
    	CCScene *s = new CCScene;
    	s->addChild(nextAction());
    	CCDirector::sharedDirector()->replaceScene(s);
    	s->release();
    }
    void HelloWorld::backCallback(cocos2d::CCObject *pSender)
    {
    	CCScene *s = new CCScene;
    	s->addChild(backAction());
    	CCDirector::sharedDirector()->replaceScene(s);
    	s->release();
    }
    void HelloWorld::menuCloseCallback(CCObject* pSender)
    {
    	CCDirector::sharedDirector()->end();
    }
    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
        return scene;
    }
    
    // on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
        bool bRet = false;
        do 
        {
            CC_BREAK_IF(! CCLayer::init());
    
    		CCSize s = CCDirector::sharedDirector()->getWinSize();
    		CCLabelTTF* l = CCLabelTTF::create("Test3333333", "Thonburi", 16);
    		addChild(l);
    		l->setPosition( ccp(s.width/2,s.height/2-100));
    		Test();
    
            bRet = true;
        } while (0);
    
        return bRet;
    }
    void HelloWorld::Test()
    {
    	CCSize s = CCDirector::sharedDirector()->getWinSize();
    	CCMenuItemImage *item1 = CCMenuItemImage::create("b1.png", "b2.png", this, menu_selector(HelloWorld::backCallback));
    	CCMenuItemImage *item2 = CCMenuItemImage::create("r1.png", "r2.png", this, menu_selector(HelloWorld::restartCallback));
    	CCMenuItemImage *item3 = CCMenuItemImage::create("f1.png", "f2.png", this, menu_selector(HelloWorld::nextCallback));
    	CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));
    	CCMenu *menu = CCMenu::create(item1, item2, item3,pCloseItem, NULL);
    	menu->setPosition(CCPointZero);
    	item1->setPosition(ccp(s.width/2 - item2->getContentSize().width*2, item2->getContentSize().height/2));
    	item2->setPosition(ccp(s.width/2, item2->getContentSize().height/2));
    	item3->setPosition(ccp(s.width/2 + item2->getContentSize().width*2, item2->getContentSize().height/2));
    	pCloseItem->setPosition(ccp(s.width - 20, s.height-20));
    	addChild(menu, 100);
    }
    Test1::Test1()
    {
    	Test();
    	CCSize s = CCDirector::sharedDirector()->getWinSize();
    	CCLabelTTF* l = CCLabelTTF::create("Test1", "Thonburi", 16);
    	addChild(l);
    	l->setPosition( ccp(s.width/2,s.height/2));
    }
    Test2::Test2()
    {
    	Test();
    	CCSize s = CCDirector::sharedDirector()->getWinSize();
    	CCLabelTTF* l = CCLabelTTF::create("Test2", "Thonburi", 16);
    	addChild(l);
    	l->setPosition( ccp(s.width/2,s.height/2));
    }
    Test3::Test3()
    {
    	Test();
    	CCSize s = CCDirector::sharedDirector()->getWinSize();
    	CCLabelTTF* l = CCLabelTTF::create("Test3", "Thonburi", 16);
    	addChild(l);
    	l->setPosition( ccp(s.width/2,s.height/2));
    }


  • 相关阅读:
    04机器学习实战之朴素贝叶斯
    06Web服务
    03机器学习实战之决策树scikit-learn实现
    03机器学习实战之决策树
    将两个列表合并为字典_其中一个列表为Key_一个列表为Value
    17反射
    16网络通信协议
    百练 2733 判断闰年 解题报告
    百练 2799 浮点数格式 解题报告
    lucene详细介绍
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211382.html
Copyright © 2020-2023  润新知