日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Cocos2d-x For WP8]Hello world

發(fā)布時間:2023/12/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Cocos2d-x For WP8]Hello world 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

[Cocos2d-x For WP8]Hello world

Cocos2d-x For WP8使用C++開發(fā),使用cocos2d-xv0.13同樣的接口,Cocos2d-x For WP8的相關(guān)項目代碼可以從下面的網(wǎng)址下載到:

https://github.com/cocos2d-x/cocos2dx-win8/tree/wp8

http://cocos2d-x.googlecode.com/files/cocos2dx-0.13.0-wp8-0.8.zip

?

打開了項目中的Hello World項目我們可以看到下面的項目結(jié)構(gòu),這里我把多余的東西刪除掉了。

圖片:

?

那么在WP8里面的Cocos2d-x框架其實還是要基于WP8的DirectX進行封裝而成的,然后映射到了Cocos2d-x框架相關(guān)的接口。

(1)程序的入口

在這個項目里面我們先來關(guān)注下cocos2dorig.cpp文件,這里封裝了程序的入口相關(guān)的信息,main方法就是程序的入口。在這個程序入口里面把原來的WP8的DirectX的初始化方法注釋掉了,換成了Cocos2d-x所封裝的程序?qū)嵗鼳ppDelegate類。

//游戲的入口 [Platform::MTAThread] int main(Platform::Array<Platform::String^>^) {//auto direct3DApplicationSource = ref new Direct3DApplicationSource();//CoreApplication::Run(direct3DApplicationSource);//創(chuàng)建程序的實例 AppDelegate App;auto frameworkViewSource = ref new CCApplicationFrameworkViewSource();Windows::ApplicationModel::Core::CoreApplication::Run(frameworkViewSource);return 0; }ref class CCApplicationFrameworkViewSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource { public:virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView(){//cocos2d::getSharedCCApplicationFrameworkView()封裝了WP8 DirectX游戲的初始化邏輯return cocos2d::getSharedCCApplicationFrameworkView();} };

(2)程序的實例

那么我們可以把AppDelegate類看作是程序的實例,因為程序的實例初始化,完成加載,程序進入休眠狀態(tài)和程序回到前臺的相關(guān)處理方法,類似于WP8應(yīng)用程序App類的功能。

//程序初始化實例 bool AppDelegate::initInstance() {bool bRet = false;do {#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN8_METRO)// fix bug: 16bit alignedvoid* buff=_aligned_malloc(sizeof(CCEGLView),16);CCEGLView* mainView = new (buff) CCEGLView();mainView->Create();//mainView->setDesignResolution(480, 320);//mainView->setDesignResolution(640, 1066);CCLOG("Device Res:%d", m_deviceResolutionInPixels);//根據(jù)三種分辨率創(chuàng)建主視圖switch (m_deviceResolutionInPixels) {case DeviceResolutionInPixels_WVGA: {mainView->setDesignResolution(480, 800);break;}case DeviceResolutionInPixels_720p: {mainView->setDesignResolution(720, 1280);break;} case DeviceResolutionInPixels_WXGA: {mainView->setDesignResolution(768, 1280);break;}}#endif // CC_PLATFORM_WIN8_METRObRet = true;} while (0);return bRet; } //程序啟動 bool AppDelegate::applicationDidFinishLaunching() {// 初始化導(dǎo)演對象CCDirector *pDirector = CCDirector::sharedDirector();//設(shè)置主視圖pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());// turn on display FPS//pDirector->setDisplayFPS(false);//設(shè)置屏幕方向pDirector->setDeviceOrientation(CCDeviceOrientationLandscapeLeft);// set FPS. the default value is 1.0/60 if you don't call this//pDirector->setAnimationInterval(1.0 / 60);// 創(chuàng)建sceneCCScene *pScene = HelloWorld::scene();// 用導(dǎo)演對象運行scene runpDirector->runWithScene(pScene);return true;}//進入休眠 void AppDelegate::applicationDidEnterBackground() {CCDirector::sharedDirector()->pause(); }//從休眠回到程序 void AppDelegate::applicationWillEnterForeground() {CCDirector::sharedDirector()->resume(); }

(3)游戲的界面和邏輯

那么HelloWorld類就是處理我們游戲的界面和邏輯的類了,話說你可以把它當(dāng)做成是MainPage類。在這里定義了默認的游戲場景。Cocos2d的游戲結(jié)構(gòu)可以簡單地概括為場景、層、精靈,而這兩個文件就是Hello World場景的實現(xiàn)文件。每個游戲組件都可以添加到另一個組件中,形成層次關(guān)系,例如場景中可以包含多個層,層中可以包含多個精靈。

HelloWorldScene中定義了一個HelloWorld類,該類繼承自CCLayerColor,CCLayerColor也就是繼承自 CCLayer,因此HelloWorld本身是一個層。

在AppDelegate::applicationDidFinishLaunching()方法里面我們看到了調(diào)用了HelloWorld::scene()的方法,那么這個就是創(chuàng)建游戲的場景。

CCScene* HelloWorld::scene() {CCScene * scene = NULL;do { // 'scene'是一個可以自動釋放的對象scene = CCScene::create();//創(chuàng)建失敗跳出循環(huán)CC_BREAK_IF(! scene);// 'layer'是一個可以自動釋放的對象HelloWorld *layer = HelloWorld::create();//創(chuàng)建失敗跳出循環(huán)CC_BREAK_IF(! layer);// 添加layer到scene上面scene->addChild(layer);} while (0);// 返回scenereturn scene; }

在上面的代碼中我們看到了調(diào)用了HelloWorld::create()的方法,那么在HelloWorldScene.h頭文件里面我們找到了該方法的定義,實際上就是調(diào)用了init()方法。

// implement the "static create()" method manually CREATE_FUNC(HelloWorld); /*** define a create function for a specific type, such as CCLayer* @__TYPE__ class type to add create(), such as CCLayer*/ #define CREATE_FUNC(__TYPE__) \ static __TYPE__* create() \ { \__TYPE__ *pRet = new __TYPE__(); \if (pRet && pRet->init()) \{ \pRet->autorelease(); \return pRet; \} \else \{ \delete pRet; \pRet = NULL; \return NULL; \} \ }

下面我們再來看看init()方法是怎么對游戲進行初始化的。

//在init方法里面初始化當(dāng)前的實例 bool HelloWorld::init() {bool bRet = false;do {//CCLayer進行初始化,初始化失敗跳出循環(huán)if ( !CCLayer::init() ){break;}//獲取手機屏幕的大小CCSize size = CCDirector::sharedDirector()->getWinSize();//創(chuàng)建文字LabelCCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Times New Roman", 24);//設(shè)置文字Label的位置pLabel->setPosition( ccp(size.width * 0.5, size.height * 0.5) );//設(shè)置文字Label的顏色pLabel->setColor(ccc3(160, 80, 5));//添加到當(dāng)前的界面上this->addChild(pLabel, 1);bRet = true;} while (0);//返回成功return bRet; }

游戲的運行如下圖所示:

?

總結(jié)

以上是生活随笔為你收集整理的[Cocos2d-x For WP8]Hello world的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。