官术网_书友最值得收藏!

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 in Objective-C checks whether something needs to be included; include does not. But we accomplish the same thing through that clever use of definitions at the top. There are other ways to run the same check (with #pragma once, for instance) but this one is added to any new C++ files you create in Xcode.
  • You can make your life easier by declaring the namespaces you'll use in the class. These are similar to packages in some languages. You may have noticed that all the uses of cocos2d:: in the code are not necessary because of the namespace declaration. But I wanted to show you the bit you can get rid of by adding a namespace declaration.
  • So next you give your class a name and you may choose to inherit from some other class. In C++ you can have as many super classes as you want. And you must declare whether your super class is public or not.
  • You declare your public, protected and private methods and members between the curly braces. HelloWorld is the constructor and ~HelloWorld is the destructor (it will do what dealloc does in Objective-C).
  • The virtual keyword is related to overrides. When you mark a method as virtual, you are telling the compiler not to set in stone the owner of the method, but to keep it in memory as execution will reveal the obvious owner. Otherwise, the compiler may erroneously decide that a method belongs to the super and not its inheriting class.

    Also, it's good practice to make all your destructors virtual. You only need use the keyword once in the super class to mark potential overrides, but it is common practice to repeat the virtual keyword in all subclasses so developers know which methods are overrides (C++11 adds a tag override, which makes this distinction even clearer, and you will see examples of it in this book's code). In this case, init comes from Layer and HelloWorld wants to override it.

        virtual bool init();
  • Oh yes, in C++ you must declare overrides in your interfaces. No exceptions!

The inline method is something new to you, probably. These methods are added to the code by the compiler wherever they are called for. So every time I make a call to addTwoIntegers, the compiler will replace it with the lines for the method declared in the interface. So the inline method works just as statements inside a method; they do not require their own bit of memory in the stack. But if you have a two-line inline method called 50 times in your program, it means that the compiler will add a hundred lines to your code.

主站蜘蛛池模板: 杂多县| 司法| 汤原县| 溧阳市| 琼结县| 海安县| 饶平县| 平遥县| 开化县| 黄山市| 安福县| 铜川市| 囊谦县| 阿克| 铁岭县| 孟连| 秀山| 高平市| 方山县| 连山| 中卫市| 江达县| 合山市| 肇庆市| 肥城市| 桦南县| 通州区| 比如县| 兖州市| 泰安市| 武城县| 保德县| 静宁县| 桂东县| 鸡西市| 石泉县| 巴彦县| 和政县| 怀宁县| 琼海市| 灵寿县|