ParallaxNode視差節點實現遠景近景的不同層次移動

來源:互聯網
上載者:User

標籤:

Cocos2d-x有ParallaxNode視差節點,視察顧名思義,就是造成不同的移動速率的效果。

我想大家都玩過刀塔傳奇,他的背景有遠景和近景之分,而且你滑動螢幕的時候遠景和近景是按照不同的速率移動的;

今天我們就來實現這個效果。

關鍵詞:ScrollView , ParallaxNode

要滑動肯定需要ScrollView了,我們一般都會用cocostudio去構建一個ScrollViewd的組件,在cocostudio裡面設定好各種我們需要的屬性,匯入工程裡面就可以直接滑動了;

cocos2d-x 3.2讀取cocostudio項目介面發生了不小的變化,cocostudio畫了一個scrollView,讀入項目;

    auto rootNode =cocostudio::timeline::NodeReader::getInstance()->createNode("ScrollViewTest_1/ScrollViewTest_1.ExportJson");

rootNode相當於一個layer,我們需要取到ScrollView來進行控制;

1 auto child = rootNode->getChildByTag(9);2     auto scroll = static_cast<ScrollView *>(child);3     scroll->setParent(nullptr);    //4     auto visiableSize = Director::getInstance()->getVisibleSize();5     scroll->setContentSize(visiableSize);   //讓ScrollView的可視範圍和螢幕大小一樣,否則會出現適配問題;

這裡需要注意的是,ScrollView的可視地區大小和可滑動地區大小的概念,可視地區相當於一個視窗,ScrollView的可滑動地區要大於可視地區,這樣才有滑動的效果;現在我們ScrollView已經準備好了。下面需要建立ParallaxNode:

 1     auto bg1 = Sprite::create("HelloWorld.png"); 2     auto bg2 = Sprite::create("HelloWorld.png"); 3     auto sprite = Sprite::create("CloseNormal.png"); 4      5     parallax = ParallaxNode::create(); 6     parallax->addChild(bg1, 1, Vec2(0.5f, 0), Vec2(100, 50));//第二個參數是zorder,第三個參數表示移動速度,第三個參數代表Child的相對位置 7     parallax->addChild(bg2, 1, Vec2(0.5f, 0), Vec2(bg1->getContentSize().width, 50)); 8     parallax->addChild(sprite, 2, Vec2(1.1, 0), Vec2(400, 300)); 9     //this->addChild(parallax);10     scroll->addChild(parallax); //最後要把視差節點放入Scrollview內

 

ParallaxNode視差節點實現遠景近景的不同層次移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.