Group Group Group Group Group Group Group Group Group Forums

Cocos2d-x Tutorial for Beginners

In this Cocos2d-x tutorial, learn how to create a basic cross-platform game for iOS, Android, and more using C++!

This is a companion discussion topic for the original entry at

What if I want to pause the game?

The call to Layer::init() in HelloWorld::init() gave me a compiler error: “Call to non-static member function without an argument”. It looks like HelloWorld inherits from cocos2d::Scene, so I called cocos2d::Scene::init() instead of Layer::init(). That got me building, but the call to Sprite::create() failed because the file couldn’t be found. I had to enter the full path to player.png to get it to work.

HelloWorld::createScene() has only one line in it: return HelloWorld::create(); Replacing that line removes the return statement, resulting in a compiler error. I tried returning the scene object instead, which fixes the compiler error, but then the game doesn’t run; I just get a black window. After poking around in the Cocos2d-x forums for a while, I found that you still have to call HelloWorld::create(), but you have to add that layer to the scene returned by Scene::createWithPhysics(), and then return that scene. So the final version of the function looks like this:

Scene* HelloWorld::createScene() { auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld()->setGravity(Vec2(0,0)); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
auto layer = HelloWorld::create();

return scene;


1 Like

onTouchBegan seems to get called twice, sending two projectiles at once. The fix seems to be to call setSwallowTouches on the eventListener:
auto eventListener = EventListenerTouchOneByOne::create();
eventListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(eventListener, _player);

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]