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

Time for action – creating the implementation

The implementation is a text file with the .cpp extension:

  1. Create a new text file and save it as HelloWorld.cpp. At the top, let's start by including our header file:
    #include "HelloWorld.h"
  2. Next, we implement our constructor and destructor:
    HelloWorld::HelloWorld () {
        //constructor
    }
    
    HelloWorld::~HelloWorld () {
        //destructor
    }
  3. Then comes our static method:
    Scene* HelloWorld::scene() {
        auto scene = Scene::create();
        
        auto layer = HelloWorld::create();
    
        scene->addChild(layer);
    
        return scene;
    }
  4. And then come our two remaining public methods:
    bool HelloWorld::init() {
        // call to super
        if ( !Layer::init() )
        {
            return false;
        }
        
        //create main loop 
        this->scheduleUpdate();
        
        return true;
    }
    
    void HelloWorld::update (float dt) {
        //the main loop
    }

What just happened?

We created the implementation for our HelloWorld class. Here are the most important bits to take notice of:

  • The HelloWorld:: scope resolution is not optional here. Every single method declared in your interface belongs to the new class that needs the correct scope resolution in the implementation file.
  • You also need the scope resolution when calling the super class like Layer::init(). There is no built-in super keyword in the standard C++ library.
  • You use this instead of self. The -> notation is used when you're trying to access an object's properties or methods through a pointer to the object (a pointer is the information of where you find the actual object in memory). The . (dot) notation is used to access an object's methods and properties through its actual instance (the blob of memory that comprises the actual object).
  • We create an update loop, which takes a float for its delta time value simply by calling scheduleUpdate. You will see more options related to this later in this book.
  • You can use the auto keyword as the type of an object if it's obvious enough to the compiler which type an object is.
  • The inline methods, of course, are not implemented in the class since they exist only in the interface.

And that's enough of syntax for now. C++ is one of the most extensive languages out there and I do not wish to leave you with the impression that I have covered all of it. But it is a language made by developers for developers. Trust me, you will feel right at home working with it.

The information listed previously will become clearer once we move on to building the games. But now, onwards to the big scary monster: memory management.

主站蜘蛛池模板: 香格里拉县| 昭苏县| 谢通门县| 嘉峪关市| 神木县| 南平市| 天门市| 黄山市| 怀宁县| 宕昌县| 平顶山市| 霍林郭勒市| 长寿区| 左云县| 密云县| 泰州市| 分宜县| 临夏市| 新乐市| 安平县| 永川市| 保定市| 天峻县| 双峰县| 南宁市| 沾化县| 三河市| 游戏| 上蔡县| 花莲市| 鹤峰县| 湾仔区| 铜山县| 海宁市| 新化县| 蒙城县| 毕节市| 馆陶县| 东乌珠穆沁旗| 革吉县| 时尚|