d入和d出菜单为了使动作更平滑,涉及到动作组合。(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);//定义背景层 d入d出//MainMenuBG MoveoutCCMoveto* MainMenuBGMoveout = CCMoveto::create(3.0f,ccp(visibleSize.wIDth/2,visibleSize.height/2));CCEaseExponentialOut* MainMenuBGExponentialOut = CCEaseExponentialOut::create(MainMenuBGMoveout);//MainMenuBG MoveinMainMenuBG = (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 instancebool 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();////设定菜单项 MenuItemCcmenuItemImage *item0 = CcmenuItemImage::create("menu_restart_normal_CN.png","menu_restart_pressed_CN.png",menu_selector(HelloWorld::menuCloseCallback));//MenuItem 1CcmenuItemImage *item1 = CcmenuItemImage::create("menu_resume_normal_CN.png","menu_resume_pressed_CN.png",menu_selector(HelloWorld::menuCloseCallback));//MenuItem 2CcmenuItemImage *item2 = CcmenuItemImage::create("menu_quit_normal_CN.png","menu_quit_pressed_CN.png",menu_selector(HelloWorld::menuCloseCallback));//MenuItem 3CcmenuItemImage *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);//定义d出 菜单d入d出效果//MainMenu moveoutCCMoveto* MainMenuMoveOut = CCMoveto::create(3.0f,visibleSize.height/2));CCEaseExponentialOut* MainMenuExponentialOut = CCEaseExponentialOut::create(MainMenuMoveOut);//MainMenu moveinMainMenu = (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);////定义背景层 d入d出//MainMenuBG MoveoutCCMoveto* MainMenuBGMoveout = CCMoveto::create(3.0f,visibleSize.height/2));CCEaseExponentialOut* MainMenuBGExponentialOut = CCEaseExponentialOut::create(MainMenuBGMoveout);//MainMenuBG MoveinMainMenuBG = (CCSprite*)this->getChildByTag(menu_bg_tag);CCPoint MainMenuBGHIDepoint = ccp(visibleSize.wIDth/2,MainMenuBGHIDepoint);CCEaseExponentialin* MainMenuBGExponentialin = CCEaseExponentialin::create(MainMenuBGMovein);//调用d入 d出动作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 d入、d出效果(以菜单为例子)全部内容,希望文章能够帮你解决cocos2d-x d入、d出效果(以菜单为例子)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)