使用Cocos2D-x引擎在Mac下开发运行在iOS设备上的《植物大战僵尸》第一篇
首先,环境搭建这方面我就不多说了,不懂的可以网上找一些资料,这方面的资料还是很多的,我采用Cocos2D-x的版本是2.2.2,环境是Mac Xcode4.6.2,因此建立工程的时候,在终端下进入/Users/tjj/Desktop/cocos2d-x/cocos2d-x-2.2.2/tools/project-creator目录下,输入如下指令:python ./create_project.py -project PlantsVSZombies -package com.hdc.edu -language cpp然后点击回车,工程即可建好,然后在projects目录下找到你所建的项目名称PlantsVSZombies,用Xcode打开即可。
下面新建一个StartLayer类,实现开始动画界面
/* *********************StartLayer.h********************* */
#ifndef __PlantsVSZombies__StartLayer__
#define __PlantsVSZombies__StartLayer__
#include <iostream>
#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::sharedDirector() -> 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);
//添加绿草地滚动动画,滚动结束之后进入到菜单界面
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(350,80));
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);
//添加草地生长的过程动作
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);//矩形
progress -> setMidpoint(ccp(0,0));//左下角,向左向上运动
CCProgressFromTo* progressTo = CCProgressFromTo::create(5, 0, 100);//这三个参数分别是执行时间,滚动条最小值,滚动条最大值
progress -> runAction(progressTo);
}
void StartLayer::replaceScene()
{
//当滚动完成之后,停止本类所有的动作,然后进入到菜单界面
this -> stopAllActions();
CCDirector::sharedDirector() -> replaceScene(MenuLayer::scene());
}
//下面进入到菜单界面
//菜单界面上有五个按钮,点击退出按钮,直接退出游戏;声音控制按钮,控制着背景音乐的开关
/* ***********************MenuLayer.h********************/
#ifndef __PlantsVSZombies__MenuLayer__
#define __PlantsVSZombies__MenuLayer__
#include <iostream>
#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::sharedDirector() -> 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);
//创建四个按钮:开始游戏、退出游戏、关于、帮助
CCMenuItemFont* startItem = CCMenuItemFont::create("开始游戏");
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("退出游戏");
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("帮助");
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("关于");
aboutItem -> setTarget(this, menu_selector(MenuLayer::clickMenu));
aboutItem -> setColor(ccc3(0, 0, 255));
aboutItem -> setFontSize(32);
aboutItem -> setTag(kMenuAbout_Tag);
aboutItem -> setFontName("Marker Felt");
aboutItem -> setPosition(ccp(160,_winSize.height/2));
CCMenu* pMenu = CCMenu::create(startItem,exitItem,helpItem,aboutItem,NULL);
pMenu -> setPosition(CCPointZero);
pMenu -> setAnchorPoint(CCPointZero);
this -> addChild(pMenu);
//另外还有一个声音的按钮,控制着背景音乐
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,70));
CCMenu* musicButton = CCMenu::create(toggle,NULL);
musicButton -> setAnchorPoint(CCPointZero);
musicButton -> setPosition(CCPointZero);
this -> addChild(musicButton);
//循环播放声音
SimpleAudioEngine::sharedEngine() -> playBackgroundMusic("background.mp3", 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;
}
//当点击帮助按钮时,进入到帮助界面
/* ********************HelpLayer.h*********************** */
#ifndef __PlantsVSZombies__HelpLayer__
#define __PlantsVSZombies__HelpLayer__
#include <iostream>
#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::sharedDirector() -> 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("返回主菜单");
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);
}
//当点击关于按钮时,进入到关于界面
/* **********************AboutLayer.h******************** */
#ifndef __PlantsVSZombies__AboutLayer__
#define __PlantsVSZombies__AboutLayer__
#include <iostream>
#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::sharedDirector() -> 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("返回主菜单");
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);
}
//然后是点击开始游戏按钮,进入到游戏界面
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。