cocos2dx2.2移植到Android

來源:互聯網
上載者:User

準備:1.vs20102.搭建好Android開發平台的eclipse3.下載Android NDK(我下的是android-ndk-r9)
我的cocos2dx解壓路徑:D:\cocos2d-x-2.2\cocos2d-x-2.2NDK路徑:D:\cocos2d-x-2.2\cocos2d-x-2.2\android-ndk-r9
步驟一、配置AndroidDNK編譯環境

運行eclipse,配置一下ndk路徑就可以了 window->Preferences,如


環境搭建好了,下面寫個測試程式驗證一下

簡單創意一個android工程,相當於helloworld,建立好後滑鼠右鍵工程名字Android Tools->Add Native Support...這時你會發現工程多了一個jni目錄


測試demo比較簡單,實現android端與C++的相互調用


步驟二、資源移植1.先將D:\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\platform\android\java\src下的org目錄拷貝到D:\cocos2d-x-2.2\cocos2d-x-2.2\projects\Helloworld\proj.android\src下2.將D:\cocos2d-x-2.2\cocos2d-x-2.2\projects\Helloworld\Classes下的全部C++檔案拷貝到D:\cocos2d-x-2.2\cocos2d-x-2.2\projects\Helloworld\proj.android\jni\hellocpp3.由於第二步轉移了C++檔案的路徑,因此在D:\cocos2d-x-2.2\cocos2d-x-2.2\projects\Helloworld\proj.android\jni下的Android.mk檔案裡也要指明這些C++檔案的路徑,要修改只有如下的項LOCAL_SRC_FILES
LOCAL_SRC_FILES := hellocpp/main.cpp \ hellocpp/AppDelegate.cpp \ hellocpp/HelloWorldScene.cpp即將(AppDelegate.cpp 和HelloWorldScene.cpp)與main.cpp的相對路徑改了
4.將路徑D:\cocos2d-x-2.2\cocos2d-x-2.2\projects\Helloworld\proj.android下的Android工程匯入Ecllipse中再將路徑D:\cocos2d-x-2.2\cocos2d-x-2.2\projects\Helloworld\Resources下的資源全部拷貝到Android工程的assets目錄下
步驟三、設定編譯方式1.右鍵移植的Android工程,選擇properties,再選擇Builders->New->program在mian頁面填入NDK編譯路徑和本工程路徑,如下:(注意第二項Working Directory 要對應自己的Android工程名稱)D:\cocos2d-x-2.2\cocos2d-x-2.2\android-ndk-r9\android-ndk-r9\ndk-build.cmd${workspace_loc:/HelloCpp}2.在Environment介面建立環境變數:內容如下:NDK_MODULE_PATHD:\cocos2d-x-2.2\cocos2d-x-2.2;D:\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\platform\third_party\android\prebuilt

步驟四、編譯運行成功

步驟五、解析度

在 AppDelegate.cpp的 bool AppDelegate::applicationDidFinishLaunching() 方法裡,加上

CCEGLView::sharedOpenGLView()->setDesignResolutionSize(576,324,kResolutionNoBorder);即可解決解析度適配問題。

其中,576,324 是筆者在win32設計遊戲時採用的寬度和高度,大家改為自己所設定的寬高即可。kResolutionNoBorder是指在螢幕展開時採用的其中一種策略,保證展開後無邊界,但是設計時介面的邊緣部分可能在最後被遮蓋。筆者通過 "go to definition"操作找到了如下的定義。以下即是全部的適配策略了。

enum ResolutionPolicy{    // The entire application is visible in the specified area without trying to preserve the original aspect ratio.    // Distortion can occur, and the application may appear stretched or compressed.    kResolutionExactFit,    // The entire application fills the specified area, without distortion but possibly with some cropping,    // while maintaining the original aspect ratio of the application.    kResolutionNoBorder,    // The entire application is visible in the specified area without distortion while maintaining the original    // aspect ratio of the application. Borders can appear on two sides of the application.    kResolutionShowAll,    kResolutionUnKnown,};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.