在d:\DevTool\cocos2d-x-2.2.2\cocos2d-x-2.2.2\tools\cocos2d-console\console
有
cocos2d_jscompile.py
cocos2d_version.py
cocos2d.py
cocos2d_new.py
d:\DevTool\cocos2d-x-2.2.2\cocos2d-x-2.2.2\tools\cocos2d-console\console>python cocos2d.py jscompile --help Usage: cocos2d.py jscompile -s src_dir -d dst_dir [-c -o COMPRESSED_FILENAME -j COMPILER_CONFIG] Options: -h, --help show this help message and exit -s SRC_DIR_ARR, --src=SRC_DIR_ARR source directory of js files needed to be compiled, supports mutiple source directory -d DST_DIR, --dst=DST_DIR destination directory of js bytecode files to be stored -c, --use_closure_compiler Whether to use closure compiler to compress all js files into just a big file -o COMPRESSED_FILENAME, --output_compressed_filename=COMPRESSED_FILENAME Only available when '-c' option was True -j COMPILER_CONFIG, --compiler_config=COMPILER_CONFIG The configuration for closure compiler by using JSON, please refer to compiler_config_sample.json
下面說一下可選參數,可選參數的意思是使用closure compiler工具壓縮代碼為一個文件。
COMPRESSED_FILENAME是壓縮后的文件名,最好使用xxx.js,因為工具會自動再后面加個c
COMPILER_CONFIG是壓縮時調用的配置文件,需要根據項目需求自己填寫,在bin目錄下有一個做好的缺省例子可以使用,compiler_config_sample.json
android
由於android下先要執行pro.android/build_native.sh腳本,所以我們可以在腳本里加入自動編譯的功能,代碼如下:
COCOS2D_CONSOLE=$COCOS2DX_ROOT/tools/cocos2d-console/console/ python $COCOS2D_CONSOLE/cocos2d.py jscompile -s assets/script/ -d assets/src/; cd assets/src; find .-type f -name "*.js"| xargs rm -rf; cd ../../;
以上代碼要放在調用NDK編譯之前。
ios
由於ios下編譯沒有對應的build_native.sh腳本,所以就沒法使用類似的方式了。幸好xcode下編譯時可以添加自定義的run script
選擇 項目 -> TARGETS -> Build Phases -> Add Build Phases -> Add Run Script,添加如下的內容:
source ~/.bash_profile CONSOLE_PATH=$COCOS2DX_ROOT"/tools/cocos2d-console/console/"; TARGET_DIR=$BUILT_PRODUCTS_DIR"/"$PRODUCT_NAME".app/"; JS_DIR=$TARGET_DIR"/src/";/usr/bin/python $CONSOLE_PATH/cocos2d.py jscompile -s $JS_DIR -d $JS_DIR; find $JS_DIR -type f -name "*.js"| xargs rm -rf;
這樣在編譯時就會自動調用該腳本。(需要在.bash_profile里定義cocos2dx的根目錄,並且變量名為COCOS2DX_ROOT)
將js編譯為字節碼發布,就不用擔心別人可以拿到js源代碼了。