Today, let's take a look at the simple version of "Plants vs. botnets" running on iOS devices using the Cocos2D-x engine development, what I do is designed with two levels, start animation, menu interface, game interface, game Success Failure interface, the Start game button, exit game button, about Button, HELP button, and pause and return button are displayed on the menu. After completing the technologies used, I will summarize them. Now I will introduce the process from.
First of all, the environment to build this I will not say much, do not understand can find some information on the Internet, this information is still a lot, I use the Cocos2D-x version is 2.2.2, the environment is Mac Xcode4.6.2, so when creating a project, enter the/Users/tjj/Desktop/cocos2d-x/cocos2d-x-2.2.2/tools/project-creator directory under the terminal, enter the following command: python. /create_project.py-project PlantsVSZombies-package com.hdc.edu-language cpp. Then, press enter to create the project. Find the project name PlantsVSZombies in the projects directory and use Xcode to open it.
Create a StartLayer class to implement the animation start interface.
/*********************** StartLayer. h **********************/
# Ifndef _ PlantsVSZombies _ StartLayer __
# Define _ PlantsVSZombies _ StartLayer __
# Include
# Include "cocos2d. h"
# Include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
Using namespace std;
Class StartLayer: public CCLayer
{
Public:
Static CCScene * scene ();
CREATE_FUNC (StartLayer );
Virtual bool init ();
Void loadUI ();
Void replaceScene ();
Private:
CCSize _ winSize;
};
# Endif
/*********************** StartLayer. cpp *********************/
# Include "StartLayer. h"
# Include "MenuLayer. h"
CCScene * StartLayer: scene ()
{
CCScene * scene = CCScene: create ();
StartLayer * layer = StartLayer: create ();
Scene-> addChild (layer );
Return scene;
}
Bool StartLayer: init ()
{
If (! CCLayer: init ())
{
Return false;
}
_ WinSize = CCDirector: shareddire()-> getWinSize ();
LoadUI ();
Return true;
}
Void StartLayer: loadUI ()
{
CCSprite * bgSprite = CCSprite: create ("titlescreen.png ");
BgSprite-> setPosition (ccp (_ winSize. width/2, _ winSize. height/2 ));
This-> addChild (bgSprite );
CCSprite * titleSprite = CCSprite: create ("pvz_logo.png ");
TitleSprite-> setPosition (ccp (_ winSize. width/2, _ winSize. height/4*3 + 30 ));
This-> addChild (titleSprite );
CCSprite * downSprite = CCSprite: create ("down.png ");
DownSprite-> setPosition (ccp (_ winSize. width/2-20, _ winSize. height/5-20 ));
This-> addChild (downSprite );
CCSprite * logoSprite = CCSprite: create ("popcap.png ");
LogoSprite-> setPosition (ccp (_ winSize. width-60, _ winSize. height/4-10 ));
LogoSprite-> setScale (0.5f );
This-> addChild (logoSprite );
// Add a green lawn scroll animation. After the scroll ends, enter the menu interface.
CCSprite * scrollgrassSprite = CCSprite: create ("scrollgrass.png ");
ScrollgrassSprite-> setPosition (ccp (70, 80 ));
ScrollgrassSprite-> setScale (0.8f );
CCRotateBy * rotateBy = CCRotateBy: create (0.05, 2*3.14 );
CCScaleTo * scaleTo = CCScaleTo: create (5, 0.3 );
ScrollgrassSprite-> runAction (CCRepeatForever: create (rotateBy ));
This-> addChild (scrollgrassSprite );
CCMoveTo * moveTo = CCMoveTo: create (5, ccp ));
CCSpawn * spawn = CCSpawn: create (moveTo, scaleTo, NULL );
CCCallFunc * func = CCCallFunc: create (this, callfunc_selector (StartLayer: replaceScene ));
CCSequence * seq = CCSequence: create (spawn, func, NULL );
ScrollgrassSprite-> runAction (seq );
// Add the growth process of Grassland
CCSprite * grassSprite = CCSprite: create ("grass.png ");
CCProgressTimer * progress = CCProgressTimer: create (grassSprite );
Progress-> setPosition (ccp (_ winSize. width/2-28, 70 ));
This-> addChild (progress );
Progress-> setType (kCCProgressTimerTypeBar); // rectangle
Progress-> setMidpoint (ccp (0, 0); // lower left corner, move to left
CCProgressFromTo * progressTo = CCProgressFromTo: create (5, 0,100); // the three parameters are execution time, minimum value of the scroll bar, and maximum value of the scroll bar respectively.
Progress-> runAction (progressTo );
}
Void StartLayer: replaceScene ()
{
// After the scroll is complete, stop all the actions of the class and enter the menu interface.
This-> stopAllActions ();
CCDirector: sharedDirector ()-> replaceScene (MenuLayer: scene ());
}
// Enter the menu interface below
// There are five buttons on the menu interface. Click the exit button to exit the game directly. The sound control button controls the background music switch.
/************************* MenuLayer. h ********************/
# Ifndef _ PlantsVSZombies _ MenuLayer __
# Define _ PlantsVSZombies _ MenuLayer __
# Include
# Include "cocos2d. h"
# Include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
Using namespace std;
Enum kMenuTag
{
KMenuStart_Tag = 1,
KMenuExit_Tag,
KMenuHelp_Tag,
KMenuAbout_Tag
};
Class MenuLayer: public CCLayer
{
Public:
Static CCScene * scene ();
CREATE_FUNC (MenuLayer );
Virtual bool init ();
Void loadUI ();
Void clickMenu (CCObject * object );
Void clickMusic ();
Private:
CCSize _ winSize;
Bool isMusic;
};
# Endif
/*********************** MenuLayer. cpp *******************/
# Include "MenuLayer. h"
# Include "SimpleAudioEngine. h"
# Include "HelpLayer. h"
# Include "AboutLayer. h"
Using namespace CocosDenshion;
CCScene * MenuLayer: scene ()
{
CCScene * scene = CCScene: create ();
MenuLayer * layer = MenuLayer: create ();
Scene-> addChild (layer );
Return scene;
}
Bool MenuLayer: init ()
{
If (! CCLayer: init ())
{
Return false;
}
_ WinSize = CCDirector: shareddire()-> getWinSize ();
IsMusic = true;
LoadUI ();
Return true;
}
Void MenuLayer: loadUI ()
{
CCSprite * bgSprite = CCSprite: create ("main_background.png ");
BgSprite-> setPosition (ccp (_ winSize. width/2, _ winSize. height/2 ));
This-> addChild (bgSprite );
// Create four buttons: start the game, exit the game, about and help
CCMenuItemFont * startItem = CCMenuItemFont: create ("Start game ");
StartItem-> setTarget (this, menu_selector (MenuLayer: clickMenu ));
StartItem-> setColor (ccc3 (0, 0,255 ));
StartItem-> setFontSize (32 );
StartItem-> setTag (kMenuStart_Tag );
StartItem-> setFontName ("Marker Felt ");
StartItem-> setPosition (ccp (_ winSize. width-150, _ winSize. height/4*3-20 ));
CCMenuItemFont * exitItem = CCMenuItemFont: create ("Quit game ");
ExitItem-> setTarget (this, menu_selector (MenuLayer: clickMenu ));
ExitItem-> setColor (ccc3 (0, 0,255 ));
ExitItem-> setFontSize (32 );
ExitItem-> setTag (kMenuExit_Tag );
ExitItem-> setFontName ("Marker Felt ");
ExitItem-> setPosition (ccp (_ winSize. width-150, _ winSize. height/2 ));
CCMenuItemFont * helpItem = CCMenuItemFont: create ("help ");
HelpItem-> setTarget (this, menu_selector (MenuLayer: clickMenu ));
HelpItem-> setColor (ccc3 (0, 0,255 ));
HelpItem-> setFontSize (32 );
HelpItem-> setTag (kMenuHelp_Tag );
HelpItem-> setFontName ("Marker Felt ");
HelpItem-> setPosition (ccp (160, _ winSize. height/4*3-20 ));
CCMenuItemFont * aboutItem = CCMenuItemFont: create ("about ");
AboutItem-> setTarget (this, menu_selector (MenuLayer: clickMenu ));
AboutItem-> setColor (ccc3 (0, 0,255 ));
AboutItem-> setFontSize (32 );
AboutItem-> setTag (kMenuAbout_Tag );
AboutItem-> setFontName ("Marker Felt ");
<G id = "1"> setPosition </G> (ccp (160, _ winSize. height/2 ));
CCMenu * pMenu = CCMenu: create (startItem, exitItem, helpItem, aboutItem, NULL );
PMenu-> setPosition (CCPointZero );
PMenu-> setAnchorPoint (CCPointZero );
This-> addChild (pMenu );
// Another sound button controls the background music.
CCMenuItemImage * musicOn = CCMenuItemImage: create ("musicbutton2.png", "musicbutton2.png ");
CCMenuItemImage * musicOff = CCMenuItemImage: create ("musicbuttonstop 2.png"," musicbuttonstop 2.png ");
CCMenuItemToggle * toggle = CCMenuItemToggle: createWithTarget (this, menu_selector (MenuLayer: clickMusic), musicOn, musicOff, NULL );
Toggle-> setPosition (ccp (70 ));
CCMenu * musicButton = CCMenu: create (toggle, NULL );
MusicButton-> setAnchorPoint (CCPointZero );
MusicButton-> setPosition (CCPointZero );
This-> addChild (musicButton );
// Loop playback sound
SimpleAudioEngine: sharedEngine ()-> playBackgroundMusic ("background.pdf", true );
}
Void MenuLayer: clickMenu (CCObject * object)
{
CCMenuItemFont * menu = (CCMenuItemFont *) object;
If (kMenuStart_Tag = menu-> getTag ())
{
}
Else if (kMenuHelp_Tag = menu-> getTag ())
{
CCTransitionMoveInT * moveInT = CCTransitionMoveInT: create (0.5, HelpLayer: scene ());
CCDirector: sharedDirector ()-> replaceScene (moveInT );
}
Else if (kMenuAbout_Tag = menu-> getTag ())
{
CCTransitionMoveInB * moveInB = CCTransitionMoveInB: create (0.5, AboutLayer: scene ());
CCDirector: sharedDirector ()-> replaceScene (moveInB );
}
Else if (kMenuExit_Tag = menu-> getTag ())
{
# 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
}
}
Void MenuLayer: clickMusic ()
{
SimpleAudioEngine * engine = SimpleAudioEngine: sharedEngine ();
IsMusic? Engine-> pauseBackgroundMusic (): engine-> resumeBackgroundMusic ();
IsMusic =! IsMusic;
}
// When you click the Help button, the Help page is displayed.
/********************** HelpLayer. h ************************/
# Ifndef _ PlantsVSZombies _ HelpLayer __
# Define _ PlantsVSZombies _ HelpLayer __
# Include
# Include "cocos2d. h"
# Include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
Class HelpLayer: public CCLayer
{
Public:
Static CCScene * scene ();
Virtual bool init ();
CREATE_FUNC (HelpLayer );
Void loadUI ();
Void back ();
Private:
CCSize _ winSize;
};
# Endif
/**************************** HelpLayer. cpp **************/
# Include "HelpLayer. h"
# Include "MenuLayer. h"
CCScene * HelpLayer: scene ()
{
CCScene * scene = CCScene: create ();
HelpLayer * layer = HelpLayer: create ();
Scene-> addChild (layer );
Return scene;
}
Bool HelpLayer: init ()
{
If (! CCLayer: init ())
{
Return false;
}
_ WinSize = CCDirector: shareddire()-> getWinSize ();
LoadUI ();
Return true;
}
Void HelpLayer: loadUI ()
{
CCSprite * bgSprite = CCSprite: create ("helpBackground.png ");
BgSprite-> setPosition (ccp (_ winSize. width/2, _ winSize. height/2 ));
This-> addChild (bgSprite );
CCMenuItemFont * backItem = CCMenuItemFont: create ("return to Main Menu ");
BackItem-> setTarget (this, menu_selector (HelpLayer: back ));
BackItem-> setPosition (ccp (_ winSize. width/2,40 ));
BackItem-> setColor (ccc3 (100,100,100 ));
BackItem-> setFontSize (32 );
CCMenu * pMenu = CCMenu: create (backItem, NULL );
PMenu-> setAnchorPoint (CCPointZero );
PMenu-> setPosition (CCPointZero );
This-> addChild (pMenu );
}
Void HelpLayer: back ()
{
CCTransitionSlideInB * slideInB = CCTransitionSlideInB: create (0.5, MenuLayer: scene ());
CCDirector: sharedDirector ()-> replaceScene (slideInB );
}
// When you click the related button, the page appears.
/*********************** AboutLayer. h *********************/
# Ifndef _ PlantsVSZombies _ AboutLayer __
# Define _ PlantsVSZombies _ AboutLayer __
# Include
# Include "cocos-ext.h"
# Include "cocos2d. h"
USING_NS_CC;
USING_NS_CC_EXT;
Using namespace std;
Class AboutLayer: public CCLayer
{
Public:
Static CCScene * scene ();
Virtual bool init ();
CREATE_FUNC (AboutLayer );
Void loadUI ();
Void back ();
Private:
CCSize _ winSize;
};
# Endif
/**************************** AboutLayer. cpp **************/
# Include "AboutLayer. h"
# Include "MenuLayer. h"
CCScene * AboutLayer: scene ()
{
CCScene * scene = CCScene: create ();
AboutLayer * layer = AboutLayer: create ();
Scene-> addChild (layer );
Return scene;
}
Bool AboutLayer: init ()
{
If (! CCLayer: init ())
{
Return false;
}
_ WinSize = CCDirector: shareddire()-> getWinSize ();
LoadUI ();
Return true;
}
Void AboutLayer: loadUI ()
{
CCSprite * bgSprite = CCSprite: create ("aboutBackground.png ");
BgSprite-> setPosition (ccp (_ winSize. width/2, _ winSize. height/2 ));
This-> addChild (bgSprite );
CCMenuItemFont * backItem = CCMenuItemFont: create ("return to Main Menu ");
BackItem-> setTarget (this, menu_selector (AboutLayer: back ));
BackItem-> setPosition (ccp (_ winSize. width/2,40 ));
BackItem-> setColor (ccc3 (0, 0,255 ));
BackItem-> setFontSize (32 );
CCMenu * pMenu = CCMenu: create (backItem, NULL );
PMenu-> setAnchorPoint (CCPointZero );
PMenu-> setPosition (CCPointZero );
This-> addChild (pMenu );
}
Void AboutLayer: back ()
{
CCTransitionSlideInT * slideInT = CCTransitionSlideInT: create (0.5, MenuLayer: scene ());
CCDirector: sharedDirector ()-> replaceScene (slideInT );
}
// Click the start game button to go to the game page.