Cocos2d-x pop-in, pop-up effect (in the menu as an example)
The pop-up and pop-up menus involve action combinations to make the action smoother. (CCMoveTo and CCEaseExponentialOut)
Take the background image of the menu as an example:
// Generate the menu background image
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 );
// Define the pop-up of the background layer
// 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 );
// Call the action
CCRepeatForever * rept0 = CCRepeatForever: create (CCSequence: create (MainMenuBGExponentialOut, MainMenuBGExponentialIn, NULL ));
MainMenuBG-> runAction (rept0 );
============================
The source code is as follows:
//////////////////////////////////////// /////
/* HelloWorldScene. h */ <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + Cjxicj4KPGJyPgojaW5jbHVkZSA = "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 );
// Define menu items
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 ();
// 'Player' is an autorelisted object
HelloWorld * layer = HelloWorld: create ();
Scene-> addChild (layers, 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...
// Call the menu
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 ();
//
// Set menu item 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 ));
// Generate the main menu based on the menu items
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 );
// Generate the menu background image
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 );
// Define the pop-up menu.
// 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 );
//
// Define the pop-up of the background layer
// 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 );
// Call the pop-up action
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;
}