cocos2dx游戏开发——微信打飞机学习笔记(五)——BackgroundLayer的搭建
生活随笔
收集整理的這篇文章主要介紹了
cocos2dx游戏开发——微信打飞机学习笔记(五)——BackgroundLayer的搭建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、創建文件~
?????? 文件名:BackgroundLayer.h
???????????????????? BackgroundLayer.cpp
?????? 架構就跟前面的一樣,我就直接進入正題 啦,而且github有完整代碼,歡迎下載~
二、創建滾動的背景
?????? 為毛要創建滾動的背景呢= =,因為我們要控制飛機,但總得有往前飛的感覺,所以呢~你懂的~
然后方法就很簡單啦,就是用2張圖片,當然是上下能對接起來的那種,然后不停的滾那滾就好啦~
方法呢,首先在.h文件中進行聲明
void moveBackground(float dt); //滾動圖片的函數Sprite *_background1; //背景圖片Sprite *_background2;然后,我們就默默實現下~
首先在init()中添加~
_background1 = Sprite::createWithSpriteFrameName("background.png");_background1->setAnchorPoint(Vec2::ZERO);_background1->setPosition(Vec2::ZERO);this->addChild(_background1);_background2 = Sprite::createWithSpriteFrameName("background.png");_background2->setAnchorPoint(Vec2::ZERO);_background2->setPosition(Vec2(_background1->getPositionX(), _background1->getPositionY() + _background1->getContentSize().height));this->addChild(_background2);這樣就初始化好那個圖片的位置啦啦~
然后就是關鍵的滾動的函數~
void BackgroundLayer::moveBackground(float dt) {if (_background2->getPositionY() <= 0){_background1->setPositionY(0);}_background1->setPositionY(_background1->getPositionY() - 2);_background2->setPositionY(_background1->getPositionY() + _background1->getContentSize().height - 2);}簡單吧~,就是一會我們不斷的調用這個函數,然后每次調用就讓圖片往下走,一旦第一張出了屏幕,就立馬放回原來的位置,就是這么簡單~
然后就是關鍵的關鍵啦~
this->schedule(schedule_selector(BackgroundLayer::moveBackground), 1.0 / 60);別看就之后一行代碼~,這是cocos封裝好的,要不很麻煩的~,現在會用就好啦,這么寫,就是在游戲過程中會以1 /60的間隔調用這個函數,這樣子的話,我們人眼看起來就是動畫的感覺~,大工告成
?
三、最后的最后
?????? 你發現運行后沒有反應~,那是因為你還沒有加入到Scene中,而且會有很多層,所以我們在GameScene中添加一個函數
//.h void initLayer();BackgroundLayer *_background;?
//.cpp void GameScene::initLayer() {//add background;_background = BackgroundLayer::create();this->addChild(_background); }四~效果圖~
?
總結
以上是生活随笔為你收集整理的cocos2dx游戏开发——微信打飞机学习笔记(五)——BackgroundLayer的搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ethereum 开发简介
- 下一篇: 3.5 卷积神经网络进阶-Incepti