盡量避免使用UI,因為要適配IOS、ANDROID、WinPhone等各種平台 => 新的一個適配各種平台,減小移植難度的遊戲引擎Cocos2D-X
Cocos2D-X 核心主要是使用C++來寫的,外圍有其他的代碼,比如OC、Android、C#
github下載Cocos2D-X
1、在window作業系統下搭建開發環境,新版本的Cocos2D-X已經沒有.bat檔案,可以從
http://download.csdn.net/detail/joejames/7331707下載一個,配置好visual studio的cocos2dx建立工程外掛程式
2、也可以參見目錄下的README.md配置path,然後用指令碼建立工程
很多遊戲的一些更新包主要使用lua指令碼要實現,這樣替換更加方便,維護起來也方便
AppDelegate.h和.cpp檔案:項目中程式的入口檔案,類似於main函數,在IOS就是程式的入口檔案
bool AppDelegate::applicationDidFinishLaunching() 程式啟動後將調用這個方法
void AppDelegate::applicationDidEnterBackground() 遊戲將要進入後台調用這個方法
void AppDelegate::applicationWillEnterForeground() 遊戲即將進入前台調用這個方法
CC裡一般名字有shared的一般都是使用單例模式,例如下面的導演類
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
遊戲元素:任何可以呈現出來的元素,例如 情境、層、精靈
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
addChild方法:把一個遊戲元素放到另一個遊戲元之中,只有把一個遊戲元素放置到其他已經呈現出來的遊戲元素中,它才會呈現出來。
CCDirector運行了scene ,scene 上的layer 就會顯示出來
virtual bool init(); 初始化方法:
//(1) 對父類進行初始化
if ( !CCLayer::init() )
{
return false;
}
//(2) 建立菜單並添加到層
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
//(3) 建立"Hello World"標籤並添加到層中
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / 2, size.height - 50));
this->addChild(pLabel, 1);
//(4) 建立顯示“HelloWorld.png”的精靈並添加到層中
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
pSprite->setPosition(ccp(size.width/2, size.height/2));
this->addChild(pSprite, 0);
bRet = true;