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.

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.


Share this article

Tagged on:         

3 thoughts on “Multitouch Asteroids game in Cocos2D-x

  • September 14, 2012 at 5:12 pm

    Having Following problem with latest version of the cocos2d-x: “cocos2d-2.0-rc2-x-2.0.1”

    //following is the older version code i got from your tutorial

    // Create sprite and add it to the layer
    CCSprite *ball = CCSprite::spriteWithFile(“Ball.jpg”);
    ball->setPosition(ccp(100, 100));

    // Create ball body
    b2BodyDef ballBodyDef;
    ballBodyDef.type = b2_dynamicBody;
    ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
    ballBodyDef.userData = ball;

    //Here is my code from the latest frame work
    //NOTE:it seems Latest frame doesn’t have get tag method for CCSprite

    _ball = CCSprite::create(p_playing_ball,CCRectMake(0,0,33,33));
    this->addChild(_ball,1, 1);
    _ball->setPosition(ccp(160, 240));
    b2BodyDef ballBodyDef;
    ballBodyDef.type = b2_dynamicBody;
    ballBodyDef.fixedRotation = false;
    //ballBodyDef.bullet = true;
    _body = _world->CreateBody(&ballBodyDef);
    //apply the shape to the ball
    b2CircleShape circle;
    circle.m_radius = 16.5/PTM_RATIO;
    //fixtureDef specifying the shape to circle
    b2FixtureDef ballShapeDef;
    ballShapeDef.shape = &circle;
    ballShapeDef.density =10.0f;
    ballShapeDef.restitution = 0.95;
    ballShapeDef.friction = 0.0f;
    ballShapeDef.filter.groupIndex =k_largeGroup;


Leave a Reply

Your email address will not be published. Required fields are marked *