Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-x 弹入、弹出效果(以菜单为例子)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
 

弹入和弹出菜单为了使动作更平滑,涉及到动作组合。(CCMoveTo 、CCEaseExponentialOut)(菜单背景图位置仅为示范例子,还需调整)

以菜单的背景图为例:

//生成菜单背景图
CCSprite* MainMenuBG = CCSprite::create("menu_bg.png");
MainMenuBG->setPosition(ccp(visibleSize.width/2 +10,visibleSize.height +20));
this->addChild(MainMenuBG,0);
MainMenuBG->setTag(menu_bg_tag);
//定义背景层 弹入弹出
//MainMenuBG Moveout
CCMoveTo* MainMenuBGMoveout = CCMoveTo::create(3.0f,ccp(visibleSize.width/2,visibleSize.height/2));
CCEaseExponentialOut* MainMenuBGExponentialOut = CCEaseExponentialOut::create(MainMenuBGMoveout);

//MainMenuBG Movein
MainMenuBG = (CCSprite*)this->getChildByTag(menu_bg_tag);
CCPoint MainMenuBGHidepoint = ccp(visibleSize.width/2,visibleSize.height + MainMenuBG->getContentSize().height/2);
CCMoveTo* MainMenuBGMovein=CCMoveTo::create(3.0f,MainMenuBGHidepoint);
CCEaseExponentialIn* MainMenuBGExponentialIn = CCEaseExponentialIn::create(MainMenuBGMovein);
//调用顺序复合动作
CCRepeatForever* rept0= CCRepeatForever::create(CCSequence::create(MainMenuBGExponentialOut,MainMenuBGExponentialIn,NULL));
MainMenuBG->runAction(rept0);


==================



源码如下:

/////////////////////////////////////////////
/*HelloWorldScene.h*/
#ifndef __HelLOWORLD_SCENE_H__
#define __HelLOWORLD_SCENE_H__


#include "cocos2d.h"


class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
    virtual bool init();  


    // there's no 'id' in cpp,so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a SELEctor callBACk
    void menuCloseCallBACk(CCObject* pSender);


//定义菜单项
bool MainMenu();


   
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};


#endif // __HelLOWORLD_SCENE_H__

//////////////////////////////////////////////////////////////////
/* HelloWorldScene.cpp */
#include "HelloWorldScene.h"


USING_NS_Cc;


enum{
menu_pause_tag =1,menu_bg_tag=2
};


CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();


    scene->addChild(layer,999);




    // return the scene
    return scene;
}


// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibLeorigin();


    /////////////////////////////
    // 2. add a menu item with "X" image,which is clicked to quit the program
    //    you may modify it.


    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png","CloseSELEcted.png",this,menu_SELEctor(HelloWorld::menuCloseCallBACk));
    
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2,origin.y + pCloseItem->getContentSize().height/2));


    // create menu,it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem,null);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu,1);


    /////////////////////////////
    // 3. add your codes below...

//调用菜单
MainMenu();

    return true;
}




void HelloWorld::menuCloseCallBACk(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCmessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}


bool HelloWorld::MainMenu()
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
//

//设定菜单项 MenuItem
CCMenuItemImage *item0 = CCMenuItemImage::create(
"menu_restart_normal_CN.png","menu_restart_pressed_CN.png",menu_SELEctor(HelloWorld::menuCloseCallBACk));
//MenuItem 1
CCMenuItemImage *item1 = CCMenuItemImage::create(
"menu_resume_normal_CN.png","menu_resume_pressed_CN.png",menu_SELEctor(HelloWorld::menuCloseCallBACk));
//MenuItem 2
CCMenuItemImage *item2 = CCMenuItemImage::create(
"menu_quit_normal_CN.png","menu_quit_pressed_CN.png",menu_SELEctor(HelloWorld::menuCloseCallBACk));
//MenuItem 3
CCMenuItemImage *item3 = CCMenuItemImage::create(
"CloseNormal.png",menu_SELEctor(HelloWorld::menuCloseCallBACk));
//根据菜单项生成主菜单
CCMenu *MainMenu = CCMenu::create(item0,item1,item2,item3,null);
MainMenu->alignItemsVerticallyWithPadding(15.0f);
MainMenu->setPosition(ccp(visibleSize.width/2,visibleSize.height));


MainMenu->setTag(menu_pause_tag);
this->addChild(MainMenu,1);


//生成菜单背景图
CCSprite* MainMenuBG = CCSprite::create("menu_bg.png");
MainMenuBG->setPosition(ccp(visibleSize.width/2 +10,0);
MainMenuBG->setTag(menu_bg_tag);


//定义弹出 菜单弹入弹出效果
//MainMenu moveout
CCMoveTo* MainMenuMoveOut = CCMoveTo::create(3.0f,visibleSize.height/2));


CCEaseExponentialOut* MainMenuExponentialOut = CCEaseExponentialOut::create(MainMenuMoveOut);

//MainMenu movein
MainMenu = (CCMenu*)this->getChildByTag(menu_pause_tag);
CCPoint MainMenuHidepoint = ccp(visibleSize.width/2,visibleSize.height + MainMenu->getContentSize().height/2);
CCMoveTo* MainMenuMovein=CCMoveTo::create(3.0f,MainMenuHidepoint);
CCEaseExponentialIn* MainMenuExponentailIn = CCEaseExponentialIn::create(MainMenuMovein);


//
//定义背景层 弹入弹出
//MainMenuBG Moveout
CCMoveTo* MainMenuBGMoveout = CCMoveTo::create(3.0f,visibleSize.height/2));


CCEaseExponentialOut* MainMenuBGExponentialOut = CCEaseExponentialOut::create(MainMenuBGMoveout);

//MainMenuBG Movein
MainMenuBG = (CCSprite*)this->getChildByTag(menu_bg_tag);
CCPoint MainMenuBGHidepoint = ccp(visibleSize.width/2,MainMenuBGHidepoint);
CCEaseExponentialIn* MainMenuBGExponentialIn = CCEaseExponentialIn::create(MainMenuBGMovein);




//调用弹入 弹出动作

CCRepeatForever* rept1= CCRepeatForever::create(CCSequence::create(MainMenuExponentialOut,MainMenuExponentailIn,NULL));
MainMenu->runAction(rept1);


CCRepeatForever* rept0= CCRepeatForever::create(CCSequence::create(MainMenuBGExponentialOut,NULL));
MainMenuBG->runAction(rept0);



return true;
}

大佬总结

以上是大佬教程为你收集整理的cocos2d-x 弹入、弹出效果(以菜单为例子)全部内容,希望文章能够帮你解决cocos2d-x 弹入、弹出效果(以菜单为例子)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。