首页 > 代码库 > Cocos2d-x_3.2 Demo ----------SpriteTest(2)

Cocos2d-x_3.2 Demo ----------SpriteTest(2)

 1 class SpriteTestDemo : public BaseTest 2 { 3 protected: 4     std::string    _title;// 没用到 5  6 public: 7     SpriteTestDemo(void); 8     virtual ~SpriteTestDemo(void); 9 10     void restartCallback(Ref* sender);    // 重置场景回调11     void nextCallback(Ref* sender);        // 下一个场景回调12     void backCallback(Ref* sender);        // 上一个场景回调13 14     // overrides15     virtual std::string title() const override;        // 标题16     virtual std::string subtitle() const override;    // 副标题17     virtual void onEnter() override;// 进入层时18 };

BaseTest类请看这里http://www.cnblogs.com/studweijun/p/3978249.html

这个类就是继承BaseTest获得切换场景和显示标题的功能,此后每个测试项类都要继承此类。我们来看下实现:

 1 #define MAX_LAYER    (sizeof(createFunctions) / sizeof(createFunctions[0]))// 测试项数 2 /* 下一个层 */ 3 Layer* nextSpriteTestAction() 4 { 5     sceneIdx++; 6     sceneIdx = sceneIdx % MAX_LAYER;// 值域0到MAX_LAYER-1 7  8     auto layer = (createFunctions[sceneIdx])();// C++11 9     return layer;10 }11 /* 上一个层 */12 Layer* backSpriteTestAction()13 {14     sceneIdx--;15     int total = MAX_LAYER;16     if( sceneIdx < 0 )17         sceneIdx += total;    18     19     auto layer = (createFunctions[sceneIdx])();20     return layer;21 }22 /* 重置层 */23 Layer* restartSpriteTestAction()24 {25     auto layer = (createFunctions[sceneIdx])();26     return layer;27 } 28 29 //------------------------------------------------------------------30 //31 // SpriteTestDemo32 //33 //------------------------------------------------------------------34 35 SpriteTestDemo::SpriteTestDemo(void): BaseTest(){}36 37 SpriteTestDemo::~SpriteTestDemo(void){}38 39 std::string SpriteTestDemo::title() const40 {41     return "No title";42 }43 44 std::string SpriteTestDemo::subtitle() const45 {46     return "";47 }48 49 void SpriteTestDemo::onEnter()50 {51     BaseTest::onEnter();  52 }53 54 void SpriteTestDemo::restartCallback(Ref* sender)55 {56     auto s = new SpriteTestScene();57     s->addChild(restartSpriteTestAction()); 
58 59 Director::getInstance()->replaceScene(s);// 替换本场景60 s->release();61 }62 63 void SpriteTestDemo::nextCallback(Ref* sender)64 {65 auto s = new SpriteTestScene();66 s->addChild( nextSpriteTestAction() );67 Director::getInstance()->replaceScene(s);68 s->release();69 }70 71 void SpriteTestDemo::backCallback(Ref* sender)72 {73 auto s = new SpriteTestScene();74 s->addChild( backSpriteTestAction() );75 Director::getInstance()->replaceScene(s);76 s->release();77 }

这个就是BestTest类的实现版,但是还是不能单独使用,因为没内容,所以还需要其他测试项继承,以便有具体的的内容,如标题、副标题等。

 

Cocos2d-x_3.2 Demo ----------SpriteTest(2)