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

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   div   

 1 class Sprite1 : public SpriteTestDemo 2 { 3 public: 4     CREATE_FUNC(Sprite1); 5     Sprite1(); 6     virtual std::string title() const override;// 主標題 7     virtual std::string subtitle() const override;// 副標題 8  9     void addNewSpriteWithCoords(Vec2 p);// 根據座標建立精靈10     void onTouchesEnded(const std::vector<Touch*>& touches, Event* event);// 觸摸結束回調11 };

SpriteTestDemo類請看這裡http://www.cnblogs.com/studweijun/p/3979976.html

這是spriteTest的第一個測試項,title()函數就是設定主標題,subtitle()函數設定副標題,addNewSpriteWithCoords函數是在根據提供的座標產生精靈。我們來看一下實現:

1 Sprite1::Sprite1()2 {3     auto listener = EventListenerTouchAllAtOnce::create();// 建立事件監聽器4     listener->onTouchesEnded = CC_CALLBACK_2(Sprite1::onTouchesEnded, this);5     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);// 指定事件的接聽程式6     7     auto s = Director::getInstance()->getWinSize();8     addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );9 }

這個建構函式功能主要是設定觸摸事件,和載入位於視窗中間的一個精靈。

 1 void Sprite1::addNewSpriteWithCoords(Vec2 p) 2 { 3     int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f);// 獲得0-13的隨機數 4    /* (idx%5,idx/5)定位到第幾幀 */ 5     int x = (idx%5) * 85;// 0-85*4 6     int y = (idx/5) * 121;// 0-121*2 7      8     auto sprite = Sprite::create("Images/grossini_dance_atlas.png", Rect(x,y,85,121) ); 9     addChild( sprite );10     11     sprite->setPosition( Vec2( p.x, p.y) );12     13     ActionInterval* action;14     float random = CCRANDOM_0_1();// 獲得0-1的浮點數15     16     /* 根據不同隨機數設定不同動作 */17     if( random < 0.20 )18         action = ScaleBy::create(3, 2);// 放縮動作19     else if(random < 0.40)20         action = RotateBy::create(3, 360);// 旋轉動作21     else if( random < 0.60)22         action = Blink::create(1, 3);// 閃爍動作23     else if( random < 0.8 )24         action = TintBy::create(2, 0, -255, -255);// 漸層動作25     else 26         action = FadeOut::create(2);// 淡出動作27     auto action_back = action->reverse();28     auto seq = Sequence::create( action, action_back, nullptr );// 序列動作29     30     sprite->runAction( RepeatForever::create(seq)/* 重複動作 */ );31 }

上面函數就是根據隨機函數獲得隨機精靈幀添加新精靈,然後隨機添加動作。

 

 1 void Sprite1::onTouchesEnded(const std::vector<Touch*>& touches, Event* event) 2 { 3     /* 迴圈獲得觸摸點 */ 4     for (auto touch: touches) 5     { 6         auto location = touch->getLocation();// 獲得觸摸位置 7      8         addNewSpriteWithCoords( location ); 9     }10 }11 /* 添加主標題 */12 std::string Sprite1::title() const13 {14     return "Testing Sprite";15 }16 /* 添加副標題 */17 std::string Sprite1::subtitle() const18 {19     return "Tap screen to add more sprites";20 }

 上面函數就是為了處理觸摸事件和設定主標題和副標題。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.