## Wednesday, October 23, 2013

### Today's Lab Of Cocos2d-X

I was experimenting to deploy my Cocos2d-X’s projects lab to my Android device. Surprisingly there were so many obstacles and ridiculous things bumped me. Here are 4 of them:

## 1. Compile configuration to Android

The configuration itself is not a piece of cake, especially on my newbie mind. Here are the step that you have to do

### Part 1. Eclipse configurations

1. Make sure you have your project (of course you have, you won’t compile it before you have something to compile, will you?)
2. Import them into your Eclipse (the proj.android folder)
3.  Import Cocos2d-X library for Android. It’s in [Your Cocos2d-X folder]/cocos2dx/platform/android/java.
4. Right click your project (NOT THE LIBRARY), and select properties. Select “Android” tab and make sure you have ticked the target.
5. Open project.properties, set the “target” to your target platform. For example I make mine this
6. Open AndroidManifest.xml, and set the android:minSdkVersion to 14
7. Right click your project folder, select “Android Tools” > “Fix Project Properties

### Part 2. Compile commands configurations

1. Go to your project’s android folder (proj.android folder).
2. Open “build_native.sh” in your favorite text editor and at the top add this line
a friend of mine told me it was unnecessary. Well it is necessary for Mac (I don’t know how to configure NDK_ROOT variable in Bash for default, but anyway, it works).
Notes: As you can see I am using r8e version of android NDK. This is the only version that can compile Cocos2d-X 2.1.4 right.
4. Open “Android.mk” file. Make sure you have put all your class in LOCAL_SRC_FILES (it will contain HelloWorld and AppDelegeate class for default). This is the example from me:
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/Ball.cpp \
../../Classes/GameScene.cpp \

5. Open “Application.mk” and add this line for targeting your desired platform
APP_PLATFORM := android-17

### Part 3. Compiling

1. Back to Eclipse. Select your folder, and run it. It will take a moment, so grab a snack or coffee. BEWARE OF ERROR (Google is your friend. No. Best Friend. No. It's your wife).
2. Select your target device and scream in joy because you’ve deployed your app in your device.

## 2. Directly instantiate the initialization

So I have this game that need background of white. I simply add white layer first
CCLayerColor *layerWithColor;
layerWithColor->create(ccc4(255, 255, 255, 255));

Well, that’s looks okay. It’s compile smoothly on my Mac target, but no in Android target. The error is trivial and pops up when you run the game. You know what? You have to do this instead:

CCLayerColor *layerWithColor = CCLayerColor::layerWithColor->create(ccc4(255, 255, 255, 255));

That's insane!

## 3. No images in my device

So I was using a sprite sheet that sized 3000x500. But unfortunately in my device it won’t drawn and shown as black box instead. Why? Because the LogCat told me that:

cocos2d: WARNING: Image (3000 x 500) is bigger than the supported 2048 x 2048

## 4. Configuring path to Box2D

One of my experiment using Box2D. To compile it you have to tweak “Android.mk” file and add Box2D in your LOCAL_C_INCLUDES. I’ve found mine like this:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \$(LOCAL_PATH)/../../../../external/Box2D \


## UNSOLVED

### Can’t call parent destructor from override

I’ve try to make game in Box2D and it require manual deallocation being called in destructor. In Mac target, I can write it like this

GameScene::~GameScene(){
delete _world;
_body = NULL;
_world = NULL;
CCLayer::~CCLayer();
}