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);


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

cocos2d-x 弹入、弹出效果(以菜单为例子)_第1张图片


源码如下:

/////////////////////////////////////////////
/*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",
this,
menu_selector(HelloWorld::menuCloseCallback));
//MenuItem 1
CCMenuItemImage *item1 = CCMenuItemImage::create(
"menu_resume_normal_CN.png",
"menu_resume_pressed_CN.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
//MenuItem 2
CCMenuItemImage *item2 = CCMenuItemImage::create(
"menu_quit_normal_CN.png",
"menu_quit_pressed_CN.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
//MenuItem 3
CCMenuItemImage *item3 = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
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,visibleSize.height +20));
this->addChild(MainMenuBG,0);
MainMenuBG->setTag(menu_bg_tag);


//定义弹出 菜单弹入弹出效果
//MainMenu moveout
CCMoveTo* MainMenuMoveOut = CCMoveTo::create(3.0f,ccp(visibleSize.width/2,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,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* rept1= CCRepeatForever::create(CCSequence::create(MainMenuExponentialOut,MainMenuExponentailIn,NULL));
MainMenu->runAction(rept1);


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



return true;
}




你可能感兴趣的:(cocos2d-x,初探)