標籤:style blog http color io os ar for div
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)