Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Building Android Games with Cocos2d-x
  • Toc
  • feedback
Building Android Games with Cocos2d-x

Building Android Games with Cocos2d-x

By : Hernandez
4.3 (8)
close
Building Android Games with Cocos2d-x

Building Android Games with Cocos2d-x

4.3 (8)
By: Hernandez

Overview of this book

If you have a basic understanding of the C++ programming language and want to create videogames for the Android platform, then this technology and book is ideal for you.
Table of Contents (10 chapters)
close
9
Index

Template code walk-through

In this section, we will explain the main parts of the Cocos2d-x template code generated in the previous sections by the project creation script.

Java classes

We now have one Java class in our project named AppActivity that has no members and extends the Cocos2dxActivity class from the core library. We can also see that 22 Java classes from the core library have been referenced in the project. This code is aimed to make our C++ code work, and we don't have to modify it at all.

Android application configuration

The generated AndroidManifest.xml, which is the android configuration file, requires the permission android.permission.INTERNET, which allows your Android application to use the Internet connection on your device; nevertheless, this is not needed by our simple game code since there is no Internet interaction. So, you may delete this line from the AndroidManifest.xml file if you wish. Your game will be shown in landscape by default, but if you wish to create a game that runs in portrait mode, then you should change the android:screenOrientation value from landscape to portrait.

In order to change the Android application name, you may modify the app_name value located on the strings.xml file; it will affect the launcher icon's text and the application identifier within the Android OS.

When you are creating your own games, you will have to create your own classes, and those will often be more than the two classes that were created by the script. Every time you create a new class, you need to add its name to the LOCAL_SRC_FILES property of the Android.mk make file located inside the jni folder of your new project directory structure. So, when your cpp code is built by the C++ build tools, it knows which files it should compile.

C++ classes

Two C++ classes have been created: AppDelegate and HelloWorldScene. The first one is in charge of launching the Cocos2d-x framework and passing the control to the developer. The framework loading process happens within this class. If the Cocos2d-x core framework is successfully launched on the target device, it will run the applicationDidFinishLaunching method, which is the first game-specific function to be run.

The code is very straightforward and it is documented such that you will be able to grasp its logic easily. Our first minor change to the code will be to hide the debug information that shows by default on this sample game. You could simply guess that in order to achieve this you should only send false as a parameter for the setDisplayStats method call in the director singleton instance, as we can see in the following code listing:

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLViewImpl::create("My Game");
        director->setOpenGLView(glview);
    }
    // turn on display FPS
    director->setDisplayStats(false);
    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);
    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene();
    // run
    director->runWithScene(scene);
    return true;
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Scenes

As we will cover in the chapters later in this book, Cocos2d-x handles the scene concept just like movies; movies are composed by scenes, so are the Cocos2d-x games. We can visualize the different screens, such as loading, main menu, world selection, gameplay levels, ending credits, and so on, as the different scenes. Each scene has a class that defines its behavior. The template code has only one scene named HelloWorld scene that is initialized and launched from within the AppDelegate class. The scene flow is managed by the game director as we have seen in the previous code. The Director class has all the basic features to drive the game, just like a director does during a movie. There is a single shared instance of the director class that is used within the whole application scope.

HelloWorldScene contains the layer that represents all the visible areas that show up when we run our HelloWorld application, that is, the hello world label, the Cocos2d-x logo, and the menu showing the exit option.

Within the init method, we do the instantiation of the visual elements, and then we add it to the scene using the addChild method inherited from the Node core class.

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete