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 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...