Compiling your Cocos2d-x 3.0 Android project on Mac

In my last post I discussed installing Cocos2d-x and creating a new project. You will find that the project should work in Xcode without any issues at all for both iOS and OSX. Anytime you add or create new files through Xcode just make sure you add them to both targets and you should be all set. To get everything working for Android is not quite as easy”.

I should also note that I absolutely despise Eclipse.  So I will be compiling the Android project using a command terminal.  I may revisit this process with Eclipse later, but not unless anyone asks.

First you are going to need both the Android SDK and the Android NDK, I suggest downloading the latest versions.  On my system I install these under “/mobile/lib” along with any other mobile related libraries.  I keep the version name in the directory because at times I do quite a bit of switching library versions.

You’ll likely need to make some changes in the Android project.  If you’ve added files to Classes you will need to make a modification to the “jni/Android.mk” file by adding or deleting you classes from the LOCAL_SRC_FILES variable.

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/GameLayer.cpp \
                   ../../Classes/GameSprite.cpp

You will also need to set NDK_ROOT in your environment, usually this is set in your “~/.bash_profile”. You will also need to set ANDROID_HOME to point to the Android SDK.  We need this later but might as well do it now as well.

export NDK_ROOT=/mobile/lib/android-ndk-r9d
export ANDROID_HOME=/mobile/lib/adt-bundle-mac-x86_64-20140321/sdk

Now you should be able to execute “python build_native.py” in the root of the Android project. The few lines of output should show that the libcocos2dcpp.so shared library is installed without any errors. Anytime you modify your project and rebuild you will have to re-execute this script.

$ python build_native.py 
...
[armeabi] Install        : libcocos2dcpp.so => libs/armeabi/libcocos2dcpp.so

To create an APK for installation on your device simply you must first update the project with your own settings.  You can do this via the “android update project” command.  I struggled with getting this working properly for Cocos2d-x 3.0 so let me know if you have any troubles.  To update and build your project:

$ $ANDROID_HOME/tools/android update project --path ../cocos2d/cocos/2d/platform/android/java --target android-19
Updated project.properties
Updated local.properties
Updated file ../cocos2d/cocos/2d/platform/android/java/proguard-project.txt
$ ant -Dsdk.dir=$ANDROID_HOME debug
...
BUILD SUCCESSFUL
Total time: 5 seconds
$ ls bin/*debug.apk
bin/AirHockey-debug.apk

Installing Cocos2d-x 3.0 on a Mac and starting a new project.

Download the official release of Cocos2d-x 3.0. Place the unzipped archive on your system.  I use /mobile/lib for anything related to mobile development.

Set your environment PATH variable to point to the new cocos script.  Usually this would go into your “~.bash_profile”. This script will create a new project template for you.  There are no more templates to setup within Xcode.

export COCOS_CONSOLE_ROOT=/mobile/lib/cocos2d-x/tools/cocos2d-console/bin
export PATH=$COCOS_CONSOLE_ROOT:$PATH

Create a new Project

$ cocos new AirHockey -p com.andytarkinson.airhockey -l cpp -d .
Runing command: new
> Copy template into /Users/andy/projects/cocos2dx/AirHockey
> Copying cocos2d-x files...
> Rename project name from 'HelloCpp' to 'AirHockey'
> Replace the project name from 'HelloCpp' to 'AirHockey'
> Replace the project package name from 'org.cocos2dx.hellocpp' to 'com.andytarkinson.airhockey'

In case the project name AirHockey sounds familiar, I’m using the book Cocos2d-x By Example, a great book by Roger Engelbert, as a basis for forcing myself to learn about the updates in Cocos2d-x 3.0.  However please be advised that this book is not yet updated for Cocos2d-x 3.0 and therefore not recommended for learning the new release.

Mobile Gaming: Are you IAPs not working?

A couple of good articles on IAPs from Pocket Gamer today.

The first article talks about what not to do.  It’s a post mortem for 3 different games and shows why they failed miserably with IAPs.  There is some good advice in there if you are looking to improve your revenue.

In-app purchase pain: How to avoid releasing the next ‘popular failure’

The next article shows where the bulk of IAP revenue comes from. Over 50% of IAPs are people spending over $10.  You may find out that a surprising number of people buy IAPs at the $50 level.

Hey big spender: 47% of mobile game IAP revenue comes from $10-$20 transactions

An important take away here is you need to THINK about IAPs, don’t just drop them into you game thinking people are going to buy them.  You have to make it obvious that you have IAPs, put them in the customers face. You need to have a compelling reason for your players to want to buy the IAPs.  They need to be an integral part of the game.

Level making for mobile puzzle games

So I’ve been thinking, as I’ve mentioned before I’m doing several puzzle games this month.  They already have a fair amount of levels built in, but these are also the types of games that can be super easy to build levels for, many per day, without too much effort.

As usual this requires some upfront work and/or help from a programmer.  But that is nothing compared to the time and money savings long term. The beauty and nature of most puzzle games (like Sudoku) is that they can be generated and solved mathematically.

For best results I suggest a 2 or 3 step process.

1)  Generate random puzzles.  Any good programmer should be able to write a program to do this.  Most info can be found through Google that will spec out the type of puzzle and the rules.

2) Tweak the puzzle to make it look better (optional).  This step does not really apply for Sudoku but could for puzzles like or similar to Klotski or Sokoban. Depending on how fancy you want to get this step does not even need any programming, you could just do it with paper and pencil.

3) Optimized puzzle solver.  This program would verify there is a solution, detail the solution and minimum number of steps to solve.  This is probably the hardest piece to have done.  But most good programmers should be able to do it.

For any of these steps there are no restrictions on what programming language to use.  Just use something that you are able to easily work with, or let your programmer suggest.  They should be able to make a Mac or PC application out of it.

This whole process can be optimized depending on the puzzle type.  Any or all of the steps could be incorporated into a single piece of software.  You will need to go through this process for each type of puzzle app you want to create.  The software can be as fancy or as crude as you like.  For some puzzle types someone may have already written a program you can use for free.  Google is your friend.

I’ll be going through this process myself soon and will post more details about it at that time.

Picking A Platform For Your Mobile Games

Apologies ahead of time for the rambling.  Everything here is just my opinion and may certainly not be 100% accurate.  Please ignore my butchering of the english language, while it is my native language it was my worst subject in school. I find it easier to talk to computers. ;-)

Picking the programming platform for your games can be a tough job, it does matter if you are a programmer.  If you have a one off project I would let the programmer you hire suggest a platform and tell you if it meets all your criteria. In this case it’s always good to compare a few opinions.  Be sure to know ALL your requirements up front.  This prevents disaster from happening.

However, if you are like me and want to produce multiple games on multiple device platforms, it is to your advantage to use the same programming environment if at all possible.  It will reduce the amount of coding required for projects after the first one.  Key factors for my evaluations are cross platform code and ease (or even possibility) of including 3rd party libraries.

Game Builders

This is an interesting group.  The platforms I’ve looked at are, Gamesalad, GameMaker and Stencyl.  I have used Gamesalad for building 2 games that are in the app store. I have not used GameMaker or Stencyl but have researched them.  Builders greatly reduce the amount or programming knowledge you need to build a game, but not necessarily the time if you are doing it all yourself.  I cannot recommend using Gamesalad going forward.  There is currently no way to integrate 3rd party libraries for monetization and the whole development environment can be awkward at times.  It appears GameMaker may have some ability to include 3rd party libraries but I cannot seem to find a ton of info on that.  Stencyl can export Objective-C or ActionScript code, so including 3rd party libraries should be possible.  In my opinion Stencyl is the only one that deserves a second look, which I may do at some point in the future.

3d Engines

Unity3d seems to be a popular choice these days, especially for 3d games (you can do 2d as well).  I have played with it somewhat but it’s a little hard for me to wrap my head around.  First off 3d environments confuse me, I’m and old school 2d guy.  I’m sure if I sat down with a few tutorials I could get a better understanding.  But the main issue for me is that it requires me to learn programming languages that I don’t already know, C# or Javascript. Neither of which are native to any current mobile devices and require 3rd party extensions to run 3rd party libraries.  This is not a bad thing necessarily as many of these extensions are supported.  For me Unity3d just seems to cumbersome for what I want to do and the environment is just to unfamiliar to me.

Another similar platform here is Shiva3d.  Again a 3d environment, so a bit unfamiliar to me.  However it uses C++ and Lua which is something more familiar to me.  Also, it is much easier to do your own integration of 3rd party libraries, far from trivial, but easier.  I am somewhat more impressed with this one.  Both Shiva and Unity deserve another look down the road when I’m ready to do something that requires 3d.  Unreal engine is another 3d platform that seems promising, I have not looked at it enough to make any comments.

Other Game Engines

Cocos2d-x is probably the most time consuming choice but the most flexible at the same time. Programming Cocos2d-x is as close to native programming for both Android and iOS that you can get while still being cross platform.  You can without too much extra effort access all native functions of each phone with few problems.  Integrating 3rd party libraries is again not trivial but fairly easy.

CoronaSDK is a fairly easy language to learn, it’s based on a language called Lua. It seems very flexible you can get access to native device functions and include 3rd party libraries if you use the enterprise version.  It is very easy (for a programmer) to create simple cross platform games.  There seems to be a good amount of support out there in the community. Once you get beyond some of the programming quirks it’s pretty nice.

Adobe Air can be used to deploy Actionscript code to mobile.  It’s got a little of it’s own set of quirky issues, but getting beyond that it’s great. There is the capability of writing your own extensions for native device support and 3rd party libraries.  It’s an old platform to some extent with much support in the community, but at the same time it’s still an evolving platform (Corona has this issue to a lesser degree) with some more recent info hard to find.  Air to mobile is fairly new so don’t expect to get up and running quickly!

Monkey is a development option that I have discovered a short time ago.  I have not tested it myself but it looks very promising. It exports to many platforms and you can translate Monkey code to a variety of other languages including C++, C#, Java, Javascript and Actionscript.  This flexibility is great for including 3rd party libraries.  This is one to keep and eye on in the future.  If I had know about this 6 months ago It may have been the best way for me to go.

The biggest missing player here is Cocos2d, at the beginning of this article I mentioned cross platform devices, specifically iOS and Android. Cocos2d is only for native iOS apps. I have deployed a single app with Cocos2d and have several others in varying degrees of completion. It uses Objective C and really is one of the best options for games if you only care about iOS.  Personally I think this is short sighted and a mistake long term.  But that choice is up to you depending on your goals.  There certainly are many good reasons to choose Cocos2d.

As of right now I’m working with CoronaSDK and Adobe Air.  Both provide support for Android and iOS.  Both are higher level programming than native code, allowing for more rapid development.  Both allow for including 3rd party libraries (albeit not easy). I don’t expect to be changing development environments in the near future.

Hope that helps someone.  Just getting my thoughts down here for my own good and yours!  If you have any suggestions, questions, flames or anything at all to say feel free to post a comment!

Quick update on this months games

So I was going to get out a post today on mobile platforms, it was actually half written over a month ago, but that will have to wait until tomorrow, not enough hours in this day at least.

I have 2 games just about ready to go.  One will be uploaded this week and another next week.  This months 4 games are all puzzle games with 30-40 levels built in already.  I’ll be releasing them as free apps with minimal ads to start.  Really I just need to get them out there to start testing the marketing.

Both iOS and Android versions will be available for all my games going forward.  That’s it for tonight, more to come soon!

Building Mobile Games: (Re)Starting My Journey

After over a year of study and research and several false starts I’ve come to a point where I am finally ready to seriously (haha!) begin my foray into building mobile games for iOS and Android.  I’ve investigated just about every game library, engine or builder out there from Gamesalad to Unity. The 7 games I have released up to now, and this post mark the end of the research and testing phase (analysis paralysis) and the beginning of getting down to business.  This past year I seen much success from the group of friends I’ve made in this business, now it is my turn.   I will have 4 new games released (or updated) this month.

The team for now, consists of me, a part time designer/artist and 1 or 2 part time programmers.  We are focused on first releasing a handful of puzzle games. This also entails creating and refining a marketing and promotion “machine” that can be “bolted” onto any game that we release.  Much thought has gone into designing a method to get some initial downloads and exposure.  We are focused first on creating quality fun games and gaining users rapidly.  There is much to test and the process will probably have to be tweaked for iOS vs. Android.  They are 2 very different beasts.

It never ceases to amaze me what can be accomplished by keeping an positive attitude and an open mind.  There truly is a solution for every problem you just have to be willing to see it.  The key to success really is all about keeping your head down and working hard (but not too hard!).

I’ll attempt to update here several times during the week, especially if I get some audience participation.  ;-)

Using Social Media for Marketing Apps

Here’s a great infographic on demographics of social media users for the most popular sites.  Really great for figuring out which site to put most of your effort and marketing budget.  For example Facebook is mostly older females and Google+ is mostly younger males.  If your business is making custom dolls, you probably want to be interacting with your customers via Facebook.  ;-)

However, once you figure out who your market is and choose the appropriate social site, don’t ignore the others.  The other sites can still provide great value in SEO for you websites and credibility to your brand and products.  But focus your user interaction primarily on where you target market hangs out.

This is useful for not only niche apps but for any online business.

Android on Mac: The Hybrid iOS/Android Project

Today we are going to set up a project for both iOS and Android using the same directory structure.  We will be able to share all the common code for both platforms.  This will greatly reduce maintenance time over keeping two separate projects in sync.

We will be setting up a new Xcode project for iOS.  We will copy the needed android files from the project we created in the last post.

Fire up Xcode and create a new Cocos2dx project with the same name in a directory other that the Cocos2dx source tree.  I use ~/Projects for all my development projects and repositories.  In this case I’m using “com.company.project” for the Bundle ID.

Now we will need to copy various files from the Android project to the Xcode project. Let’s set a few environment variables to reduce the clutter then copy over the needed files and directories.

XCODEPROJ=~/Projects/project/project
CC2DXROOT=~/src/cocos2dx
DROIDPROJ=${CC2DXROOT}/project

cp -R ${DROIDPROJ}/proj.android ${XCODEPROJ}/android
cp -R ${CC2DXROOT}/cocos2dx ${XCODEPROJ}/libs/
cp -R ${CC2DXROOT}/CocosDenshion ${XCODEPROJ}/libs

Within the android folder (~/Projects/project/project/android) you copied to you Xcode project you will need to make the following edits to build_native.sh.

COCOS2DX_ROOT=../libs
GAME_ROOT=../
GAME_ANDROID_ROOT=$GAME_ROOT/android

Now you can verify that we didn’t break anything. Build and test the project within Xcode as normal. Also build and test the android project as we did in the previous post. Make sure you are inside the android directory of the Xcode project!

Next post we will discuss setting up Xcode so that we can do at least some of these steps for Android within Xcode itself!

  1. Android on Mac: Cocos2dx, Android SDK and CrystaX NDK
  2. Android on Mac: The Hybrid iOS/Android Project

Android on Mac: Cocos2dx, Android SDK and CrystaX NDK

My main goal in this series is to develop Android and iOS apps with Cocos2dx. I only want to use Xcode. I have no interest at all in trying to use Eclipse.

First step for developing for Android on the Mac is to install all the required libraries.  We will need the latest versions of Cocos2dx (cocos2d-rc2-x-2.0.1), Android SDK (android-sdk_r20.0.1-macosx) and CrystaX NDK (android-ndk-r7-crystax-5.beta2-darwin-x86).  The versions in the parenthesis are the ones I have used for this exercise.  I have chosen the CrystaX NDK over the standard Android NDK for the various added features such as C++11 and Obj-C support. Feel free to pick your preference.

Create a “src” directory off you home directory. Place the three packages you downloaded into the ~/src directory and unpack them.  The “~” represents you home directory (/Users/yourname/). Bring up a terminal window and create a few links as shown here.

$ cd ~/src
$ ls -F
android-ndk-r7-crystax-5.beta2/
android-sdk-macosx/
cocos2d-2.0-rc2-x-2.0.1/

$ ln -s android-ndk-r7-crystax-5.beta2 android-ndk
$ ln -s android-sdk-macosx android-sdk
$ ln -s cocos2d-2.0-rc2-x-2.0.1 cocos2dx

$ ls -F
android-ndk@                    
android-ndk-r7-crystax-5.beta2/
android-sdk@
android-sdk-macosx/
cocos2dx@
cocos2d-2.0-rc2-x-2.0.1/

The links help keep the library paths a little more readable and easier to manage.  I keep various older versions of all these libraries around for troubleshooting purposes. It’s easy to swap them out.  I just create a link to the version of the library I want to use.

Complete the install of Cocos2dx as follows:

$ cd ~/src/cocos2dx
$ ./install-templates-xcode.sh -u

Add the following to your ~/.bash_profile, create this file if it does not exist. This will setup your environment for the remainder of this series.

export NDK_ROOT=~/src/android-ndk
export ANDROID_SDK_ROOT=~/src/android-sdk
export NDK_ROOT_LOCAL=$NDK_ROOT
export COCOS2DX_ROOT_LOCAL=~/src/cocos2dx
export PATH=${PATH}:${ANDROID_SDK_ROOT}/tools:${ANDROID_SDK_ROOT}/platform-tools

Now either bring up a NEW terminal window or type “source ~/.bash_profile” at the command prompt. If everything was done correctly entering “android” at the command prompt will bring up the sdk installer. Select and install the following packages below.

Installing the packages will take a while. There is no installation required for the CrystaX NDK (or Android NDK) other than what we have already done.  Once complete let’s test that things are working properly. We’ll create an Android Cocos2d project, compile the C++ files and assemble an apk file.

Create a new Android project (with my responses as well):

$ cd ~/src/cocos2dx
$ ./create-android-project.sh

Input package path. For example: org.cocos2dx.example
com.company.project

# I chose the target id that corresponded to "android-16"
input target id:
1

input your project name:
project

Build apk file:

$ cd ~src/cocos2dx/project/proj.android
$ ./build_native.sh
$ ant debug

If you get a failure similar to “Error: No resource found that matches the given name (at ‘icon’ with value ‘@drawable/icon’)” you will need to edit the AndroidManifest.xml file and change “drawable/icon” to “drawable/ic_launcher”.  I had to do this for the specific library versions I used to create this post.

Test in Android emulator (not working for me using all these newer libraries, app crashes when I try to start it):

$ emulator @project &
$ adb install -r bin/project-debug.apk

Test on real device! Make sure you enabled USB Debugging (Settings > Applications > Development) on the device:

$ ant debug install

Great! If you’ve made it this far you have a working Andriod build system. Coming up in the next couple of posts we will create a hybrid iOS/Android project, integrate an Android target into Xcode and finally test on a real device (hopefully the emulator as well).