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

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

 1 class SpriteBatchNode1: public SpriteTestDemo 2 { 3 public: 4     CREATE_FUNC(SpriteBatchNode1); 5     SpriteBatchNode1(); 6     void addNewSpriteWithCoords(Vec2 p);// 根据新坐标创建精灵 7     void onTouchesEnded(const std::vector<Touch*>& touches, Event* event);// 触摸结束回调 8  9     virtual std::string title() const override;// 主标题10     virtual std::string subtitle() const override;// 副标题11 };

这个类和SpriteTest(2.1)的类差不多,只不过构造函数里实现由差异,来看一下实现:

 1 SpriteBatchNode1::SpriteBatchNode1() 2 { 3     auto listener = EventListenerTouchAllAtOnce::create(); 4     listener->onTouchesEnded = CC_CALLBACK_2(SpriteBatchNode1::onTouchesEnded, this); 5     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); 6  7     auto BatchNode = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 50);// 创建精灵集合对象 8     addChild(BatchNode, 0, kTagSpriteBatchNode); 9     10     auto s = Director::getInstance()->getWinSize();11     addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );12 }

上面函数里面添加精灵集合对象,其他可以参考SpriteTest(2.1)。

 1 void SpriteBatchNode1::addNewSpriteWithCoords(Vec2 p) 2 { 3     auto BatchNode = static_cast<SpriteBatchNode*>( getChildByTag(kTagSpriteBatchNode) );// 根据标志获取节点 4      5     int idx = CCRANDOM_0_1() * 1400 / 100; 6     int x = (idx%5) * 85; 7     int y = (idx/5) * 121; 8      9 10     auto sprite = Sprite::createWithTexture(BatchNode->getTexture()/* 获得纹理 */, Rect(x,y,85,121));// 从精灵集合类中获取纹理11     BatchNode->addChild(sprite);12 13     sprite->setPosition( Vec2( p.x, p.y) );14 15     ActionInterval* action;16     float random = CCRANDOM_0_1();17     18     if( random < 0.20 )19         action = ScaleBy::create(3, 2);20     else if(random < 0.40)21         action = RotateBy::create(3, 360);22     else if( random < 0.60)23         action = Blink::create(1, 3);24     else if( random < 0.8 )25         action = TintBy::create(2, 0, -255, -255);26     else 27         action = FadeOut::create(2);28 29     auto action_back = action->reverse();30     auto seq = Sequence::create(action, action_back, nullptr);31     32     sprite->runAction( RepeatForever::create(seq));33 }
除了注释的其他可以参考SpriteTest(2.1)。

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