cocos2dx游戏开发——捕鱼达人mini版学习笔记(二)——MainMenu的搭建

一、创建文件~

        MainMenuScene.h   MainMenuScene.cpp   MainMenuLayer.h   MainMenuLayer.cpp

       那个场景的搭建就不多说了,那个我的打飞机还有别踩白块的学习笔记里有~

二、How to do?

1、initBackground(),创建背景~

(1)在init中先获得屏幕的大小,还有加入图片进入缓存

visibleSize = Director::getInstance()->getVisibleSize();

    origin = Director::getInstance()->getVisibleOrigin();



    //add texture to cache

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("MainMenu/UI_GameMenuText_cn-hd.plist");

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("MainMenu/UI_GameStartMenuLayer-hd.plist");

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("MainMenu/FishActor-Small-hd.plist");

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("MainMenu/FishActor-Marlin-hd.plist");

(2)实现方法~

void MainMenuLayer::initBackground()

{

    //set background

    auto ui_background = Sprite::create("MainMenu/ui_background_normal-hd.png");

    ui_background->setAnchorPoint(Vec2::ZERO);

    ui_background->setPosition(Vec2::ZERO);

    this->addChild(ui_background);



    //Set game logo

    auto ui_Logo = Sprite::create("MainMenu/main_ui_title_cn-hd.png");

    ui_Logo->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height / 1.35f));

    this->addChild(ui_Logo, 2);

}

      记得最后要加入到init里面去哦~

(3)效果图~

image

2、加入按钮~

(1)方法实现~

//Create the start button of the game

    auto startGameBtn = Sprite::createWithSpriteFrameName("ui_button_box02_02.png");

    auto startGameBtnPush = Sprite::createWithSpriteFrameName("ui_button_box02_01.png");

    auto startGameFont = Sprite::createWithSpriteFrameName("ui_2p_010.png");



    //Create the scene choose button

    auto sceneChooseBtn = Sprite::createWithSpriteFrameName("ui_button_box01_02.png");

    auto sceneChooseBtnPush = Sprite::createWithSpriteFrameName("ui_button_box01_01.png");

    auto sceneChooseFont = Sprite::createWithSpriteFrameName("button_other_014.png");



    //Create the menu

    auto startGameMenuItem = MenuItemSprite::create(startGameBtn, startGameBtnPush, CC_CALLBACK_1(MainMenuLayer::startGameEvent, this));

    auto sceneChooseMenuItem = MenuItemSprite::create(sceneChooseBtn, sceneChooseBtnPush, CC_CALLBACK_1(MainMenuLayer::sceneChoose, this));

    sceneChooseMenuItem->setPosition(Point(startGameMenuItem->getPosition().x, startGameMenuItem->getPosition().y - 140));

    auto startGameMenu = Menu::create(startGameMenuItem, sceneChooseMenuItem, NULL);



    //Set the posiiton of menu

    startGameMenu->setPosition(Point(visibleSize.width / 2.0f, visibleSize.height / 1.35f-230));

    startGameFont->setPosition(Point(visibleSize.width / 2.0f, visibleSize.height / 1.35f-220));

    sceneChooseFont->setPosition(Point(visibleSize.width / 2.0f, visibleSize.height / 1.35f-370));



    //Add the menu into the scene

    this->addChild(startGameMenu, 2);

    this->addChild(startGameFont, 3);

    this->addChild(sceneChooseFont, 3);

(2)效果图~

image

3、加入泡泡~

(1)粒子效果的加入~

ParticleSystemQuad* MainMenuLayer::createPaopao(Vec2 position)

{

    //Create the paraticle of bubble

    auto paopao = ParticleSystemQuad::create("MainMenu/lizhi_qipao.plist");



    //Set the bubble position type form the ground

    paopao->setPositionType(ParticleSystemQuad::PositionType::RELATIVE);



    paopao->setPosition(position);

    paopao->setScale(2.0f);



    return paopao;

}

          在这里大家看到粒子特效的文件是已经完成的,存在一个plist文件里面而已,所以我们以后可以通过一些辅助的软件进行制作,

(2)创建泡泡

void MainMenuLayer::initBubble()

{

    auto paopaoLeft = createPaopao(Vec2(0, 0));

    this->addChild(paopaoLeft, 4);



    //Create the bubble on the lower right corner

    auto paopaoRight = createPaopao(Vec2(visibleSize.width, 0));

    this->addChild(paopaoRight, 4);

}

(3)上效果图

image

4、上鱼群~

(1)鱼群的动作的创建~

a.一群小鱼~

ActionInterval* MainMenuLayer::createFishMoveAction(FishActor *fish)

{



    //Let the matrix of fishes move back and forth

    return RepeatForever::create(

        Sequence::create(

        MoveTo::create(12, Point(fish->getPositionX() - 1300, fish->getPositionY())), CallFunc::create(CC_CALLBACK_0(MainMenuLayer::turnBack, this, fish)),

        MoveTo::create(8, Point(fish->getPositionX() + 1000, fish->getPositionY())), CallFunc::create(CC_CALLBACK_0(MainMenuLayer::turnBack, this, fish)), NULL));

}
void MainMenuLayer::turnBack(Node* sender)

{



    if (sender->getRotation() == 0.0f)

    {



        sender->setRotation(180.00f);

    }

    else 

    {



        sender->setRotation(0.00f);

    }

}

        上面就是创建一个鱼从右游到左,再从左移到右的动画~

b、大鱼

         大鱼的动画就是看到的只有从左到右~

ActionInterval* MainMenuLayer::createMarlinMoveAction(MarlinsFishActor *fish)

{



    //Let the marlin fis move behind the matrix of fishes

    return RepeatForever::create(

        Sequence::create(

        MoveTo::create(12, Point(fish->getPositionX() - 1300, fish->getPositionY())), CallFunc::create(CC_CALLBACK_0(MainMenuLayer::marlinTurnBack, this, fish)),

        MoveTo::create(8, Point(fish->getPositionX() + 1000, fish->getPositionY())), CallFunc::create(CC_CALLBACK_0(MainMenuLayer::marlinTurnBack, this, fish)), NULL));

}
void MainMenuLayer::marlinTurnBack(Node *sender)

{



    if (sender->getRotation() == 0.0f)

    {



        sender->setVisible(true);

        sender->setRotation(180.00f);

    }

    else 

    {



        sender->setVisible(false);

        sender->setRotation(0.00f);

    }

}

(2)上鱼群

void MainMenuLayer::fishActorsInital()

{



    //Create fishes

    for (int fishIndex = 0; fishIndex < 7; fishIndex++) {//同种类鱼的条数



        auto smallFishActor = FishActor::createWithType(FishActor::FishActorType::SmallFish);

        auto angelFishActor = FishActor::createWithType(FishActor::FishActorType::AngelFish);

        auto croakerFishActor = FishActor::createWithType(FishActor::FishActorType::Croaker);

        auto amphiprionFishActor = FishActor::createWithType(FishActor::FishActorType::Bream);

        auto breamFishActor = FishActor::createWithType(FishActor::FishActorType::SmallFish);



        //Set the position of the fishes like a matrix

        smallFishActor->setPosition(Vec2(2000 - visibleSize.width / 10 * (fishIndex + 1), visibleSize.height / 6));

        angelFishActor->setPosition(Vec2(2000 - visibleSize.width / 10 * (fishIndex + 1),  visibleSize.height / 6 * 2));

        croakerFishActor->setPosition(Vec2(2000 - visibleSize.width / 10 * (fishIndex + 1), visibleSize.height / 6 * 3));

        amphiprionFishActor->setPosition(Vec2(2000 - visibleSize.width / 10 * (fishIndex + 1), visibleSize.height / 6 * 4));

        breamFishActor->setPosition(Vec2(2000 - visibleSize.width / 10 * (fishIndex + 1), visibleSize.height / 6 * 5));



        smallFishActor->runAction(createFishMoveAction(smallFishActor));

        angelFishActor->runAction(createFishMoveAction(angelFishActor));

        croakerFishActor->runAction(createFishMoveAction(croakerFishActor));

        amphiprionFishActor->runAction(createFishMoveAction(amphiprionFishActor));

        breamFishActor->runAction(createFishMoveAction(breamFishActor));





        //Add the fishes into the scene

        this->addChild(smallFishActor, 1);

        this->addChild(angelFishActor, 1);

        this->addChild(croakerFishActor, 1);

        this->addChild(amphiprionFishActor, 1);

        this->addChild(breamFishActor, 1);

    }



    auto marlin = FishActor::createWithType(FishActor::FishActorType::MarlinsFish);

    marlin->setVisible(false);

    marlin->setPosition(Vec2(1000, visibleSize.height / 2));

    marlin->runAction(MainMenuLayer::createMarlinMoveAction((MarlinsFishActor*)marlin));

    this->addChild(marlin, 1);

}

(3)上效果图~

image

你可能感兴趣的:(cocos2dx)