你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> 使用Cocos2D

使用Cocos2D

編輯:關於IOS

今天,讓我們一起來看一下關於使用Cocos2D-x引擎開發運行在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
#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
#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
#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
#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);
}

//然後是點擊開始游戲按鈕,進入到游戲界面

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved