---
# System prepended metadata

title: Cocos2d-x lua 環境建置 (by Hank)
tags: [女神]

---

---
tags: 女神
---

# Cocos2d-x lua 環境建置 (by Hank)

1. 從 Z 槽複製 3.10 版本
2. 安裝 python 2
3. 通過指令創建項目
``` cmd
py cocos.py new demo -p com.udream.demo -l lua -d D:\project
```
3. 啟動項目編譯
> "D:\project\demo\frameworks\runtime-src\proj.win32\demo.sln"
4. 編譯除錯
 * 在 AudioCache 中加入 
 ``` cpp
 #include <functional>
 ```
5. 設定 debug 環境
    * 在 vs code 中安裝 luaide-lite
    * 設定 launch.json 
    ``` json
    {
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Cocos2-launch",
            "type": "lua",
            "request": "launch",
            "runtimeType": "Cocos2",
            "localRoot": "${workspaceRoot}",
            "commandLine": "-workdir ${workspaceRoot} -file src/main.lua",
            "port": 7003,
            "exePath": "${workspaceRoot}/simulator/win32/demo.exe",
            "fileExtNames": [
                ".lua",
                ".txt",
                ".lua.txt",
                ".bytes"
            ],
            "isFoxGloryProject": false,
            "printType": 1
        }
    ]
    ```
6. 在 main.lua 加入 debug 片段
    ``` lua
    local breakInfoFun,xpCallFun
    if os.platform == "windows" then
        breakInfoFun,xpCallFun =  require("LuaDebug")("localhost",7003)
        cc.Director:getInstance():getScheduler():scheduleScriptFunc(breakInfoFun, 0.5, false)
        cc.FileUtils:getInstance():setPopupNotify(false)
    end
    ```
    
    require("LuaDebug")("localhost", 7003)
	
## screen size 調整

1. 打開 Cocos Code IDE support
	frameworks\runtime-src\Classes\ide-support\CodeIDESupport.h

	打開後可以在視窗右鍵調整大小

	``` cpp
	// define 1 to open Cocos Code IDE support, 0 to disable
	#define CC_CODE_IDE_DEBUG_SUPPORT 1
	```

2. 把 Config.json 複製到 Simulator 資料夾下

## How to build APK

1. Android Studio 裝起來
2. 環境變數設定
	* SDK
	* NDK (cocos2dx 3.10 版本限定 android-ndk-r10e版本)
	* ANT
> 可以透過 cocos2dx 專案下的 setup.py 設定
![](https://i.imgur.com/0QjBmdC.jpg)

3. prebuild cocoslib
``` cmd
py -2 "C:\Users\Hank\Documents\cocos2d-x-3.10\tools\cocos2d-console\bin\cocos.py" compile -p android --android-studio --app-abi x86
```

4. 開啟 Android Studio 並開啟 pro.android-studio 項目
5. 設定 build.gradle

#### Project proj.android-studio
``` java
buildscript {  
	repositories {  
		jcenter()  
		maven { url 'https://maven.google.com' }  //新增
	}   
	dependencies {  
		classpath 'com.android.tools.build:gradle:2.3.3'  //修改成 2.3.3版
  
		// NOTE: Do not place your application dependencies here; they belong  
		// in the individual module build.gradle files   
	}  
}  
  
allprojects {  
   repositories {  
		jcenter()  
		maven { url 'https://maven.google.com' }  //新增
	}
}
```

#### Your Project
``` java
compileSdkVersion 26  //目前測試此版本可用
buildToolsVersion "26.0.2"  //目前測試此版本可用
  
defaultConfig {  
   applicationId "com.udream.demo"  
   minSdkVersion 10  
   targetSdkVersion 26  //目前測試此版本可用
   versionCode 1  
   versionName "1.0"  
}
```

#### libcocos2dx
``` java
compileSdkVersion 26  //目前測試此版本可用
buildToolsVersion "26.0.2"  //目前測試此版本可用
useLibrary 'org.apache.http.legacy'  //新增
  
defaultConfig {  
   minSdkVersion 10  
   targetSdkVersion 26  //目前測試此版本可用
   versionCode 1  
   versionName "1.0"  
}

```

6. 修改 gradle-wrapper.properties
``` java
distributionBase=GRADLE\_USER\_HOME  
distributionPath=wrapper/dists  
zipStoreBase=GRADLE\_USER\_HOME  
zipStorePath=wrapper/dists  
distributionUrl=https\\://services.gradle.org/distributions/gradle-3.3-all.zip //修改成此版本

```

7. Run
* 使用 Android 手機插線
* 使用模擬器

{%hackmd BJrTq20hE %}