The Pong Demos


If you have tried creating a new -x project, you will see, that at first glance the basic application template looks a lot like the -objc template. You have an AppDelegate and a HelloWorldScene class. You have a resource folder, and you have some various system stuff.

Main difference of course being, that the classes are now .cpp files, instead of .m files.

If you look a bit closer though, you will see that the AppDelegate does not look anything like the -objc AppDelegate. Reason for this is, that the new AppDelegate is now a true cross platform file, so anything having to do with iOS, has been removed. This is moved to a class named AppController, which looks a lot more like the -objc AppDelegate, however, if you changed setup here, it would not be cross platform, so you should avoid doing that.

Instead, setting up the basics, you should now do it through the cross platform AppDelegate.cpp. The most basic is customizing initGLContextAttrs, where you now setup pixel, depth and stencil format. The next level, like showing debug stats and setting up update intervals, are done in applicationDidFinishLaunching. Much like in them old days, just with a slightly different syntax.

If you look a bit more into applicationDidFinishLaunching, you will find some resolution setup, which is new to -objc programmers. For now just leave it.

Finally, the first scene is created and started, same as in -objc.

Next, a word on memory handling in cocos2d-x.

Much have been said on the pitfalls of C++ memory handling, but the good thing is, that cocos2d-x works pretty much like -objc before ARC. While it might feel intimidating to some, to lose ARC, it really is not a big problem. Using the create() scheme objects are default created as autoreleased, and automatically retained and released, when added and removed from containers, and if you are going to build anything more complex than HelloWorld, you will need to understand the basics of memory handling anyways.

Try to compare the pong demo in -objc, with the pong demo in -x, and you will see how small the difference actually is.

The Pong demos

To get a better understanding of how cocos2d-x works compared to -objc, please download the pong-objc and the pong-x demos.

In the -x demo, I have shown a couple of different ways to set up the Scene / Layer hierarchy, plus how to create a class. You will notice, that one of the few things different from -objc, is the CRETE_FUNC macro, which automatically implements the create method as a default factory method. In the class Credits, I have shown how it is done the normal C++ way.

The idea with the demos are, that you can use them to see how things are done in both -objc and -x. Some things, like touch handling, works slightly different, but I have tried to cover a wide range of techniques, so you can compare how things are done.

If you stumble upon bugs, or have suggestions, please let me know at the forum