./create-android-project.sh
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 »
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.
The code is available as always on GitHub: AsteroidsCocos2D-x. Continue reading »
Although this was briefly covered in , I feel that CCMutableArray warrants its own article, much like CCMutableDictionary which will be addressed a bit later.
Declaring a CCMutableArray
NSArrays and NSMutableArrays were a breeze to use, you could store almost anything in them without much regard for the type of the object as long as it was derived from NSObject, so you could easily end up with an array that stored objects of multiple type.
That’s not quite the way CCMutableArrays work. The way I see it, they actually store the data in a std::vector and when you declare a new vector you decide on the type of objects it contains. This means that when you declare a new array you are going to have to do the following:
CCMutableArray<CCSprite *> *sprites = new CCMutableArray<CCSprite *>;
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.
In Objective C calling a selector after a given delay is a piece of cake as this is built in NSObject. Everyone who has touched the iPhone SDK has one time or another come across the following code:
[self performSelector:@selector(doSomething) withObject:nil afterDelay:0.5f];
If you haven’t got a clue what that does, it calls the doSomething method on the current object after a 0.5 second delay. Useful when you want to perform an action after the user does something or to hide a notification after a while.
It’s not that particularly complicated to do the same thing in C++, but you need to make use of some of Cocos2D-x features such as CCSequence, CCDelayTime and CCCallFunc.
First, let’s look at the code:
// set up the time delayCCDelayTime *delayAction = CCDelayTime::actionWithDuration(0.5f);
// perform the selector callCCCallFunc *callSelectorAction = CCCallFunc::actionWithTarget(this,
callfunc_selector(HelloWorld::doSomething));
// run the actionthis->runAction(CCSequence::actions(delayAction,
callSelectorAction,
NULL));
First, you need to set up the time delay using a CCDelayTime action. Simple enough, you send the duration of the intended delay as an argument to the method.
Then, you need to set up the selector call action. The first argument you are going to send is the object to perform the action on, the second one is a pointer to the function from that object. After the time delay action expires, the doSomething method will be called on the object represented by this. In my case, this is the scene layer.
If you are wondering, callfunc_selector is a macro that simply casts the pointer of the argument it receives to a SEL_CallFunc. This is what this baby looks like:
#define callfunc_selector(_SELECTOR) (SEL_CallFunc)(&_SELECTOR)Finally, we call runAction on the current scene. The runAction method takes as a parameter a CCSequence object. This CCSequence object allows you to chain together multiple sequential actions.
You must separate your action by a comma, and ALWAYS close the list of actions with a NULL. Failure to do so will crash your app as there is no way for CCSequence to know when to stop executing actions. This is especially true for Android, and especially weird on iOS where it doesn’t crash, but what you need to keep in mind is to always close an enumeration of actions with a NULL.
Hope this helps you Cocos2D-x noobs
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
$(LOCAL_PATH)/../../../cocos2dx
to
$(LOCAL_PATH)/../../libs/cocos2dx
Notice that the last ../ has been replaced by libs/.
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:
org.cocos2dx.boxtest
Choose an Android version (Android 2.1-update1 works well enough, that should be option 5).
Finally, input the project name:
BoxTest
A new folder called BoxTest will now show up in the Cocos2D-x directory with the following contents:

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 »
For the past few weeks I have been steadily porting and posting Cocos2D tutorial code to Cocos2D-x, mostly from Ray Wenderlich‘s awesome tutorials.
You may ask why, what is the purpose of this porting? Well, for once, I believe that having the ability to compare code side-by-side is a great learning tool. Cocos2D developers out there are probably going to find it easier than I did to migrate since they can now compare the final code and see what maps to where.
On the latest roundup, the following projects with Cocos2D-x are available on GitHub:
I will continue to update this listing as more tutorials get ported. If you would like to see any specific Cocos2D tutorial ported to Cocos2D-x, please let me know in the comments and I will try to make it happen.
What’s next?
I have in plan to port at least one Box2D tutorial, and to tell you the truth, the How To Create A Game Like Tiny Wings tutorial from Ray has been flirting with me for the past few days.
I will also try to outline my impressions so far of working with Cocos2D-x (CCMutableArray, CCMutableDictionary I am looking at you!). There are some good parts, but there also are some serious hurdles that make development less enjoyable than it should be.
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.
