Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – creating the interface


The interface, or header file, is just a text file with the .h extension.

  1. Create a new text file and save it as HelloWorld.h. Then, enter the following lines at the top:

    #ifndef __HELLOWORLD_H__
    #define __HELLOWORLD_H__
    #include "cocos2d.h" 
  2. Next, add the namespace declaration:

    using namespace cocos2d;
  3. Then, declare your class name and the name of any inherited classes:

    class HelloWorld : public cocos2d::Layer {
    
  4. Next, we add the properties and methods:

    protected:
    int _score;
    
    public:
    
        HelloWorld();
        virtual ~HelloWorld();
    
        virtual bool init();
        static cocos2d::Scene* scene();
        CREATE_FUNC(HelloWorld);
        void update(float dt);
        inline int addTwoIntegers (int one, int two) {
            return one + two;
        }
    };
  5. We finish by closing the #ifndef statement:

    #endif // __HELLOWORLD_H__

What just happened?

You created a header file in C++. Let's go over the important bits of information:

  • In C++ you include, you do not import. The import statement...