cocos2d-x 3.6版連連看開始介面,cocos2d-x3.6

來源:互聯網
上載者:User

cocos2d-x 3.6版連連看開始介面,cocos2d-x3.6

上一節講了一個loading的動畫介面,其實沒有loading資源。
怎麼樣loading資源,如何預先載入資源呢。

直接上代碼

 // 建立一個一直重複的動畫    loading->runAction(RepeatForever::create(Sequence::create(fadeIn, fadeIn->reverse() ,NULL)));    // loading resources    {        // 幀緩衝        auto spriteFrameCache = SpriteFrameCache::getInstance();        // 紋理緩衝        auto textureCache = Director::getInstance()->getTextureCache();        textureCache->addImage(s_backgound);        textureCache->addImage(s_backgound_cloud1);        textureCache->addImage(s_backgound_cloud2);        textureCache->addImage(s_backgound_sea1);        textureCache->addImage(s_backgound_sea2);        textureCache->addImage(s_beach_adornment);        textureCache->addImage(s_island);        spriteFrameCache->addSpriteFramesWithFile(s_boat_plist);        // 載入聲音資源        SimpleAudioEngine::getInstance()->preloadBackgroundMusic(s_music_Back2new);        SimpleAudioEngine::getInstance()->preloadBackgroundMusic(s_music_Back3new);        SimpleAudioEngine::getInstance()->preloadBackgroundMusic(s_music_class);        SimpleAudioEngine::getInstance()->preloadBackgroundMusic(s_music_mainmenu);        SimpleAudioEngine::getInstance()->preloadEffect(s_music_2);        SimpleAudioEngine::getInstance()->preloadEffect(s_music_3);        SimpleAudioEngine::getInstance()->preloadEffect(s_music_4);        SimpleAudioEngine::getInstance()->preloadEffect(s_music_12);        SimpleAudioEngine::getInstance()->preloadEffect(s_music_14);        SimpleAudioEngine::getInstance()->preloadEffect(s_music_15);        SimpleAudioEngine::getInstance()->preloadEffect(s_music_16);        SimpleAudioEngine::getInstance()->preloadEffect(s_music_19);    }

這段預先載入資源是寫在init()函數裡面的,接著上一章講的代碼。
可以看出,這裡的載入資源是同步載入。就是init以後,一個一個去載入資源,一個載入完了載入另一個。
可以在開始載入的地方打個時間,載入完的地方再打個時間,算一下載入資源需要多少時間。
擷取系統時間,單位秒

utils::gettime();

這個載入資源比較快,不到一秒就載入完畢了。

載入完畢以後需要進入情境了,就是開始情境。

那這裡如何?這個機制。

scheduleOnce(SEL_SCHEDULE(&StartGame::initUi), 2);

載入完以後直接用一個自訂調度器,調了一個函數,initUi,延遲2秒調用。

簡單粗暴。

在這個initUi()函數裡面,我會把遊戲開始前的情境實現完成。大概有一個海洋背景,幾片雲朵,倒影,島嶼,船等。

void StartGame::initUi(float t){    removeAllChildren();    auto textureCache = Director::getInstance()->getTextureCache();    auto spriteFrameCache = SpriteFrameCache::getInstance();    {// 載入背景        auto background = Sprite::createWithTexture(textureCache->getTextureForKey(s_backgound));        background->setPosition(wSize.width / 2, wSize.height / 2);        addChild(background);    }    {   // 白雲1        auto cloud1 = Sprite::createWithTexture(textureCache->getTextureForKey(s_backgound_cloud1));        // 設定錨點,左下角        cloud1->setAnchorPoint(Vec2(0, 0));        cloud1->setPosition(0, wSize.height - cloud1->getContentSize().height);        addChild(cloud1);        // 白雲倒影        auto cloudShadow = Sprite::createWithTexture(textureCache->getTextureForKey(s_backgound_cloud1));        cloudShadow->setAnchorPoint(Vec2(0, 0));        cloudShadow->setFlippedY(true);        cloudShadow->setOpacity(40);        cloudShadow->setPosition(40, wSize.height - cloud1->getContentSize().height * 2);        addChild(cloudShadow);        // 白雲2        auto cloud2 = Sprite::createWithTexture(textureCache->getTextureForKey(s_backgound_cloud2));        // 設定錨點,左下角        cloud2->setAnchorPoint(Vec2(0, 0));        cloud2->setPosition(cloud1->getContentSize().width, wSize.height - cloud2->getContentSize().height);        addChild(cloud2);        // 島        auto land = Sprite::createWithTexture(textureCache->getTextureForKey(s_island));        land->setAnchorPoint(Vec2(1, 0));        land->setPosition(wSize.width - 40, wSize.height - land->getContentSize().height - 47 * 2);        addChild(land);        // 島倒影        auto landShadow = Sprite::createWithTexture(textureCache->getTextureForKey(s_island));        landShadow->setAnchorPoint(Vec2(1, 0));        landShadow->setFlippedY(true);        landShadow->setOpacity(40);        landShadow->setPosition(wSize.width - 40, wSize.height - land->getContentSize().height - 78 * 2);        addChild(landShadow);        // 取第一幀        auto frame = spriteFrameCache->getSpriteFrameByName("sailing_boat1.png");        auto boat = Sprite::createWithSpriteFrame(frame);        boat->setPosition(wSize.width - boat->getContentSize().width-50*2,230*2);        // 建立一個幀動畫        auto animation = Animation::create();        for (int i = 1; i < 4; i++) {            char bname[64] = {0};            sprintf(bname, "sailing_boat%d.png", i);            animation->addSpriteFrame(spriteFrameCache->getSpriteFrameByName(bname));        }        animation->setDelayPerUnit(0.5);        animation->setRestoreOriginalFrame(true);        addChild(boat);        auto animate = Animate::create(animation);        boat->runAction(RepeatForever::create(animate));        // 船來回遊盪,並會迴轉        auto flipxAction = FlipX::create(true);        auto moveBy = MoveBy::create(10, Vec2(-240, 0));        auto action = Sequence::create(moveBy, flipxAction, moveBy->reverse(), flipxAction->reverse(), NULL);        boat->runAction(RepeatForever::create(action));        // 第二條船        auto boat2 = Sprite::createWithSpriteFrame(frame);        boat2->setFlippedX(true);        boat2->setPosition(100, 400);        addChild(boat2);        boat2->runAction(animate->clone());        auto moveBy2 = MoveBy::create(12, Vec2(300, 0));        auto action2 = Sequence::create(moveBy2, flipxAction->clone()->reverse(), moveBy2->reverse(), flipxAction->clone(), NULL);        boat2->runAction(RepeatForever::create(action2));    }}

3.x版的動作系統和2.x有很大區別,比如MoveTo動作沒有reverse()方法了,就是說MoveTo不能直接逆動作了。還有幀動畫簡化了很多。

大概會是這麼個樣子:

還有一些元素,下一張再來說明。

聯繫我們

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