Xcode Project Structure
If we create a new Cocos2d-x project, we will see that four files (two classes) contain the code for thas particular project. They are:
AppDelegate.h AppDelegate.cpp HelloWorldScene.h HelloWorldScene.cpp
The AppDelegate class contains the code that controls the resolution and general settings of the game. Anything that needs to be initialized once in the app can happen in this class.
The HelloWorldScene class contains all the code that is responsible for the things that we see on the screen when the game is running. We will remove almost everything that is shown on the screen from the code to create a clean empty project for our tests. Feel free to rename this class to whatever and rename the files too, but remember to change the #include line in the AppDelegate too.
There is also a resource folder that holds our images, sprite sheets, sounds, music and fonts. The source of Cocos2d-x itself is also included in the project.
Lets create a project, call it “empty”, and have a look at the files. We will use this as a template project in the next posts to start with.
cocos new empty -p com.games.empty -l cpp -d ~/Projects
This is how it looks in Xcode if you open the “empty.xcodeproj” file from “proj.ios_mac” subfolder in the project directory.
The AppDelegate and The Director Class
The Director class is what we use to control the global setting of the game, the main window, the main look and running the scenes. Things like resolution, frame rate, program instance are all accessible through this class.
If you look at your AppDelegate.cpp, inside applicationDidFinishLaunching method you will find a few lines that uses the director.
First, it gets an instance of the director singleton class:
// initialize director auto director = Director::getInstance();
This line turns the display of frame rate and other info On or Off for your game:
// turn on display FPS director->setDisplayStats(true);
This line sets the frame rate to 60 frames per second:
// set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0f / 60);
Finally, this line starts the game with running the first scene:
// run director->runWithScene(scene);
You will see more of this later when we have a look at scenes and layers, but for the moment, you can have a look at the method list of the Director class in the class reference to get a better idea of what is possible or not with this class.
Clean Up The HelloWorldScene Class
This is the class that draws everything you see on the screen when you run a new project.
As we are going to test pretty much everything in this class, we need to remove all the things that comes with it by default. All the images you see on the screen when you run a new project, needs to be removed. To do so, go to your HelloWorldScene.cpp and remove pretty much everything from HelloWorld::init() so that it looks like this:
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Scene::init() ) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // We will add our code here return true; }
You can also delete this callback function that was used by the exit botton:
void HelloWorld::menuCloseCallback(Ref* pSender) { //Close the cocos2d-x game scene and quit the application Director::getInstance()->end(); /*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() as given above,instead trigger a custom event created in RootViewController.mm as below*/ //EventCustom customEndEvent("game_scene_close_event"); //_eventDispatcher->dispatchEvent(&customEndEvent); }
If you run your project now, you should just see a blank screen and the stats printed on it!
We will use this template every time that we want to test something new by adding our code before “return true”. I’ve kept the “visibleSize” and the “origin” variables as we will need those coordinates to place sprites and other objects in our scene.