Cocos2d-x is a multiplatform game framework. It is a reimplementation of Cocos2D for iPhone in C++.
The main idea about it is that you are going to write plain old C++ code and that code will run on both Android (via JNI thanks to the NDK) and iPhone. It also has support for Win32, WoPhone and some other platforms, check them out on the Cocos2D-X website.
The zip that you can download from the project’s website includes wizards/helpers for creating a new project for each of the supported platforms, but it doesn’t have a wizard that will let you specify what platforms do you want to work on and then generate everything from one shot.
I took some time and figured out a way to manually create one of these hybrid projects for Android and iOS. This article is actually based on an edited version of an older article of mine which addressed an older version of cocos2dx and mainly assumed that you were using Xcode 3. Continue reading »
Continue reading »
One more Ray Wenderlich tutorial ported over to Cocos2D-x.
The Breakout tutorial has always been very dear to me – it was the first tutorial that showed me how simple but powerful Box2D can be.
If you are following the latest tutorials on Ray’s site, especially the catapult game tutorial you are going to find some very familiar code in the touch handling section .
Anyway, I am going to cut this short just tell you that the code is as always available on GitHub: BreakoutCocos2D-x.
A new tutorial has been ported over to Cocos2D-x for you folks!
I stumbled upon an Asteroids game tutorial from Ganbaru Games from December 2010. Asteroids game clones have always served as excellent teaching guides for aspiring web developers. You can quickly learn about devising simple but clear user interfaces, basic physics and playing with moving objects with different speeds and orientations.
Thanks to some really hard work from Gustavo Ambrozio from raywenderlich.com we now have a new tutorial to explore the intriguing world of Box2D and Cocos2D.
In the first part of his How To Make a Catapult Shooting Game with Cocos2D and Box2D tutorial, Gustavo takes us through the very first steps of setting up the Box2D world, creating the catapult using revolute joints, arming it and launching the projectile. Although this may seem like quite a lot of work, it is actually quite simple to grasp.
As always, I have ported the tutorial code to Cocos2D-x. I tried to keep in line with Gustavo’s naming convention, however, I could not bear to use member variables without marking them in some way. By the way, I went with Cocos2D-x‘s convention of naming the members with Continue reading »
m_ as a prefix.
Continue reading »
For those of you who have followed my Creating an iPhone and Android Cocos2d-x hybrid project tutorial please pay attention to the paths. When you see a path that references the libraries directory (the folder that contains the cocos2dx, Box2D and CocosDenshion folders) you need to change the path from
Notice that the last
../ has been replaced by
Creating the project
To create the project, fire up a Terminal, navigate to the Cocos2D-x directory and create a new Android project:
Of course if you are on Windows, you are probably going to use create-android-project.bat.
Give the project a package path such as:
Choose an Android version (Android 2.1-update1 works well enough, that should be option 5).
Finally, input the project name:
Now, you are going to need some Box2D code in there. You can take that code by creating a new Box2D project using the wizard for iPhone or Win32 and copying over the files in the Classes folder. Make sure you also copy the image files in the Resources folder. Continue reading »
Continue reading »
This went faster than I expected… I just completed porting the second part of the How To Make a Tile Based Game with Cocos2D tutorial by Ray Wenderlich. The sources are available, as always, on GitHub.
I also updated to Cocos2D-x 0.9.1 and apparently there have been quite a lot of API changes. I couldn’t figure out a way to update my installation automatically so I just recreated the project and copied my game classes files over to the updated project folder.
The issues that I mentioned in the previous post with Android are still manifesting themselves in the newest version of the framework. I have tried with various resolutions and the outlines around the tiles appear only in high resolutions (like 800×480).
I tried this both on the devices (a G1 and an SGS) and the simulator and the behavior is consistent. I will put together a test case and post it on the Cocos2D-x forums, maybe someone can tell me if I am doing something wrong.
As I am moving forward with Ray Wenderlich’s awesome Cocos2D tutorials I have just completed Ray’s How To Make a Tile Based Game with Cocos2D Part 1 tutorial.
The code has been, as always, committed to GitHub.
However, the iOS build works perfectly. I am thinking this is a bug in Cocos2D-x 0.9.0, when I get the chance I will update to 0.9.1 to see if something changes. Speaking of which… now I need to figure out how to update from one Cocos2D-x version to another.
Well this has been… difficult to find.
In Cocos2D-iPhone to enable the horizontal orientation for your game you need to do the following in the AppDelegate of your project:
The process is rather similar in Cocos2D-x, but we’ll get to that in a moment.
The recommended way to set the orientation in an Android game is to use the
android:screenOrientation="landscape" property in AndroidManifest.xml.
If you open the AppDelegate.cpp file you will find there two methods that are called when the game is launched:
AppDelegate::initInstance()– this method was supposed to be called by all the platforms
AppDelegate::applicationDidFinishLaunching()– this method was supposed to be called by iPhone only
Now, you see that I said “was supposed”. For some reason the
applicationDidFinishLaunching() method is called by the other platforms as well.
initInstance() method is supposed to, of course, initialize the instance for each of the platforms based on preprocessor directives. When you get to the iOS section you get:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. #endif // CC_PLATFORM_IOS
And the Android section:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp // the default setting is to create a fullscreen view // if you want to use auto-scale, please enable view->create(320,480) in main.cpp #endif // CC_PLATFORM_ANDROID
The rest of the platforms usually initialize and set their preferred orientation and window size here. iOS on the other hand does that in the
Now, the problem with this is that this code will get executed by Android as well, and if you set the orientation here to landscape, that means that it will conflict with the already set orientation from the Android manifest file.
To get around this, you need to make sure this code only gets executed when building for iOS. We will of course use the handy preprocessor directives to isolate this piece of code:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); #endif
And that’s it! The game should now run in landscape in both Android and iPhone. You may want to follow the instructions for the Android build related to the main.cpp file – your game will look funky otherwise.
The workflow to run the Cocos2D-x projects on Android is as follows:
- Open the project in Eclipse (File > New Android Project > Create project from existing source, select the android folder from your project folder)
- Open a terminal and navigate to the android folder, run
- When that’s done, switch back to Eclipse and clean the project (Project > Clean)
- Right click the project and select Run As > Android Application
At this point if your Android device is connected, the app should automatically be deployed to it if the API versions match.
If you do not have an Android device, now’s a good time to configure a new Android Virtual Device (AVD).
Every time you modify the source files you are going to need to run build_native (step 2) in the terminal and then clean the project in Eclipse (step 3). Cleaning the project will force Eclipse to refresh the bundle files and take into account the newly compiled code when creating the game APK.
By default, the
create-android-project.sh wizard will not set the min SDK version in the manifest file. It is a good idea to have that set so open up the
AndroidManifest.xml file and set it to the oldest version that you want to support. I usually go with SDK version 7 (Android 2.1-update1).
If you want to manually edit the XML file you will need to add the following line right above the
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7"></uses-sdk>