Musings of amateur game development

Menu

Multitouch Asteroids game in Cocos2D-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.

Lessons learned

Enabling multitouch in Cocos2D-x

As evidenced in this wiki article, multitouch is automatically enabled on Android, but it needs manual activation on iOS by editing the AppController file and adding the following line after the EAGLView *__glView initialization:

[__glView setMultipletouchEnabled:YES];

std::string sucks compared to NSString

std::string is a pain to use. What’s a simple [NSString stringWithFormat:@"%d", i] in Objective C you are going to find yourself jumping through hoops to do the same thing with C++ strings.

C++ call graph is less obvious than it should be

C++‘s inheritance model is different than Objective C‘s. The tutorial suggested that we extend CCSprite and override the initWithTexture: method. If you step through the code, calling [Ship spriteWithFile:] causes the following methods to be called:

spriteWithFile: (static)

alloc

initWithFile:

initWithTexture:rect:

Extending CCSprite with a new class called Ship and overriding initWithTexture:rect:, with a call to super’s initWithTexture:rect:, will have the following call graph in Objective C when allocating a new instance:

[CCSprite spriteWithFile:] (static)

[CCSprite alloc]

[CCSprite initWithFile:]

[Ship initWithTexture:rect:]

[CCSprite initWithTexture:rect:]

The same thing in C++ appears to yield:

CCSprite::spriteWithFile (static)

CCSprite::alloc

CCSprite::initWithFile

CCSprite::initWithTexture

I may be missing something, but annoying nonetheless. I ended up overriding spriteWithFile and setting my initial values and scheduleUpdate call on the instance that I just allocated.